Code

f74f595d7e8782899737e108b5ff5d5b8ef043bf
[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_ntp_config",
8     "new_ldap_config",
9     "new_key",
10     "generate_hw_digest",     # no implementations
11     "detect_hardware",
12     "confirm_new_key",
13     "ping",
14     "import_events",    # no implementations
15     );
16 @EXPORT = @events;
18 use strict;
19 use warnings;
20 use Data::Dumper;
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";
29 my %cfg_defaults = (
30     "client" => {
31         "ldap" => [\$ldap_enabled, 1],
32         "offline-ldap" => [\$offline_enabled, 0],
33         "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
34         "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
35         "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
36         "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
37     },
38 );
40 BEGIN {}
42 END {}
44 ### Start ######################################################################
46 &main::read_configfile($main::cfg_file, %cfg_defaults);
49 my $server_address = $main::server_address;
50 my $server_key = $main::server_key;
51 my $client_mac_address = $main::client_mac_address;
53 sub write_to_file {
54     my ($string, $file) = @_;
55     my $error = 0;
57     if( not defined $file || not -f $file ) {
58         &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
59         $error++;
60     }
61     if( not defined $string || 0 == length($string)) {
62         &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
63         $error++;
64     }
65     
66     if( $error == 0 ) {
68         chomp($string);
69             
70         if( not -f $file ) {
71             open (FILE, "$file");
72             close(FILE);
73         }
74         open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
75         print FILE $string."\n";
76         close(FILE);
77     }
79     return;    
80 }
83 sub get_events {
84     return \@events;
85 }
87 sub daemon_log {
88     my ($msg, $level) = @_ ;
89     &main::daemon_log($msg, $level);
90     return;
91 }
93 sub registered {
94     my ($msg, $msg_hash) = @_ ;
96     my $header = @{$msg_hash->{'header'}}[0];
97     if( $header eq "registered" ) {
98         my $source = @{$msg_hash->{'source'}}[0];
99         &main::daemon_log("INFO: registration at $source",1);
100         $main::server_address = $source;
101     }
103     # set globaly variable client_address
104     my $target =  @{$msg_hash->{'target'}}[0];
105     $main::client_address = $target;
107     # set registration_flag to true 
108     my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
109      # Write the MAC address to file
110     if(stat($main::opts_file)) { 
111             unlink($main::opts_file);
112     }
114     my $opts_file_FH;
115     my $hostname= $main::client_dnsname;
116     $hostname =~ s/\..*$//;
117     $hostname =~ tr/A-Z/a-z/;
118     open($opts_file_FH, ">$main::opts_file");
119     print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
120     print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
121     print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
122     print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
123     if(defined(@{$msg_hash->{'ldap_available'}}) &&
124                    @{$msg_hash->{'ldap_available'}}[0] eq "true") {
125         print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
126         }
127     if(defined(@{$msg_hash->{'error'}})) {
128                 my $errormsg= @{$msg_hash->{'error'}}[0];
129         print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
130                 &write_to_file($errormsg, $fai_logpath);
131         }
132     close($opts_file_FH);
133      
134     my $out_msg = &create_xml_string($out_hash);
135     return $out_msg;
138 sub server_leaving {
139     my ($msg_hash) = @_ ;
140     my $source = @{$msg_hash->{'source'}}[0]; 
141     my $header = @{$msg_hash->{'header'}}[0];
142     
143     daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
144     $main::server_address = "none";
145     $main::server_key = "none";
147     # reinitialization of default values in config file
148     &main::read_configfile;
149     
150     # registrated at new daemon
151     &main::register_at_server();
152        
153     return;   
156 ## @method new_ntp_config
157 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
158 # @param msg - STRING - xml message with tag server
159 # @param msg_hash - HASHREF - message information parsed into a hash
160 sub new_ntp_config {
161     my ($msg, $msg_hash) = @_ ;
163     # Sanity check of incoming message
164     if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
165         &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
166         return;
167     }
169     # Fetch the new ntp server from incoming message
170     my $ntp_servers = $msg_hash->{'server'};
171     &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
172     my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
173     my $found_server_flag = 0;
175     # Sanity check of /etc/chrony/chrony.conf
176     if (not -f $chrony_file) {
177         &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
178         return;
179     }
181     # Substitute existing server with new ntp server
182     open (FILE, "<$chrony_file");
183     my @file = <FILE>;
184     close FILE;
185     foreach my $line (@file) {
186         if ($line =~ /^server /) {
187             if ($found_server_flag) {
188                 $line =~ s/^server [\s\S]+$//;
189             } else {
190                 $line =~ s/^server [\s\S]+$/$ntp_servers_string/;
191             }
192             $found_server_flag++;
193         }
194     }
196     # Append new server if no old server configuration found
197     if (not $found_server_flag) {
198         push(@file, "\n# ntp server configuration written by GOsa-si\n");
199         push(@file, $ntp_servers_string);
200     }
202     # Write changes to file and close it
203     open (FILE, "+>$chrony_file");
204     print FILE join("", @file); 
205     close FILE;
206     &main::daemon_log("INFO: wrote new configuration file: $chrony_file", 5);
208     # Restart chrony deamon
209     my $res = qx(/etc/init.d/chrony force-reload);
210     &main::daemon_log("INFO: restart chrony daemon: $res", 5);
212     return;
216 sub new_ldap_config {
217     my ($msg, $msg_hash) = @_ ;
219     if( $ldap_enabled != 1 ) {
220             return;
221     }
223     my $element;
224     my @ldap_uris;
225     my $ldap_base;
226     my @ldap_options;
227     my @pam_options;
228     my @nss_options;
229     my $goto_admin;
230     my $goto_secret;
231     my $admin_base= "";
232     my $department= "";
233     my $release= "";
234     my $unit_tag;
236     # Transform input into array
237     while ( my ($key, $value) = each(%$msg_hash) ) {
238         if ($key =~ /^(source|target|header)$/) {
239                 next;
240         }
242         foreach $element (@$value) {
243                 if ($key =~ /^ldap_uri$/) {
244                         push (@ldap_uris, $element);
245                         next;
246                 }
247                 if ($key =~ /^ldap_base$/) {
248                         $ldap_base= $element;
249                         next;
250                 }
251                 if ($key =~ /^goto_admin$/) {
252                         $goto_admin= $element;
253                         next;
254                 }
255                 if ($key =~ /^goto_secret$/) {
256                         $goto_secret= $element;
257                         next;
258                 }
259                 if ($key =~ /^ldap_cfg$/) {
260                         push (@ldap_options, "$element");
261                         next;
262                 }
263                 if ($key =~ /^pam_cfg$/) {
264                         push (@pam_options, "$element");
265                         next;
266                 }
267                 if ($key =~ /^nss_cfg$/) {
268                         push (@nss_options, "$element");
269                         next;
270                 }
271                 if ($key =~ /^admin_base$/) {
272                         $admin_base= $element;
273                         next;
274                 }
275                 if ($key =~ /^department$/) {
276                         $department= $element;
277                         next;
278                 }
279                 if ($key =~ /^unit_tag$/) {
280                         $unit_tag= $element;
281                         next;
282                 }
283                 if ($key =~ /^release$/) {
284                         $release= $element;
285                         next;
286                 }
287         }
288     }
290     # Unit tagging enabled?
291     if (defined $unit_tag){
292             push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
293             push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
294             push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
295     }
297     # Setup ldap.conf
298     my $file1;
299     my $file2;
300     open(file1, "> $ldap_config");
301     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
302     print file1 "URI";
303     foreach $element (@ldap_uris) {
304         print file1 " $element";
305     }
306     print file1 "\nBASE $ldap_base\n";
307     foreach $element (@ldap_options) {
308         print file1 "$element\n";
309     }
310     close (file1);
311     daemon_log("wrote $ldap_config", 5);
313     # Setup pam_ldap.conf / libnss-ldap.conf
314     open(file1, "> $pam_config");
315     open(file2, "> $nss_config");
316     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
317     print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
318     print file1 "uri";
319     print file2 "uri";
320     foreach $element (@ldap_uris) {
321         print file1 " $element";
322         print file2 " $element";
323     }
324     print file1 "\nbase $ldap_base\n";
325     print file2 "\nbase $ldap_base\n";
326     foreach $element (@pam_options) {
327         print file1 "$element\n";
328     }
329     foreach $element (@nss_options) {
330         print file2 "$element\n";
331     }
332     close (file2);
333     daemon_log("wrote $nss_config", 5);
334     close (file1);
335     daemon_log("wrote $pam_config", 5);
337     # Create goto.secrets if told so - for compatibility reasons
338     if (defined $goto_admin){
339             open(file1, "> /etc/goto/secret");
340             close(file1);
341             chown(0,0, "/etc/goto/secret");
342             chmod(0600, "/etc/goto/secret");
343             open(file1, "> /etc/goto/secret");
344             print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
345             close(file1);
346             daemon_log("wrote /etc/goto/secret", 5);
347     }
349     # Write shell based config
350     my $cfg_name= "/etc/ldap/ldap-shell.conf";
352     # Get first LDAP server
353     my $ldap_server= $ldap_uris[0];
354     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
356     open(file1, "> $cfg_name");
357     print file1 "LDAP_BASE=\"$ldap_base\"\n";
358     print file1 "LDAP_SERVER=\"$ldap_server\"\n";
359     print file1 "LDAP_URIS=\"@ldap_uris\"\n";
360     print file1 "ADMIN_BASE=\"$admin_base\"\n";
361     print file1 "DEPARTMENT=\"$department\"\n";
362     print file1 "RELEASE=\"$release\"\n";
363     print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
364     print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
365     close(file1);
366     daemon_log("wrote $cfg_name", 5);
368     # Write offline config
369     if ($offline_enabled){
370             $cfg_name= "/etc/ldap/ldap-offline.conf";
372             # Get first LDAP server
373             open(file1, "> $cfg_name");
374             print file1 "LDAP_BASE=\"$ldap_base\"\n";
375             print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
376             print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
377             print file1 "ADMIN_BASE=\"$admin_base\"\n";
378             print file1 "DEPARTMENT=\"$department\"\n";
379             print file1 "RELEASE=\"$release\"\n";
380             print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
381             print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
382             close(file1);
383             daemon_log("wrote $cfg_name", 5);
384     }
386     return;
390 sub new_key {
391     # my ($msg_hash) = @_ ;
392     my $new_server_key = &main::create_passwd();
394     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
395     my $out_msg = &create_xml_string($out_hash);
397     # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
398     # comes from gosa-si-server within a given time
399     
401     return $out_msg; 
405 sub confirm_new_key {
406     my ($msg, $msg_hash) = @_ ;
407     my $header = @{$msg_hash->{'header'}}[0];
408     my $target = @{$msg_hash->{'target'}}[0];
409     my $source = @{$msg_hash->{'source'}}[0];
411     &main::daemon_log("confirm new key from $source", 5);
412     return;
417 sub detect_hardware {
419     &write_to_file('goto-hardware-detection-start', $fai_logpath);
421         my $hwinfo= `which hwinfo`;
422         chomp $hwinfo;
424         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
425                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
426                 return;
427         }
429         my $result= {
430                 gotoHardwareChecksum => &main::generate_hw_digest(),
431                 macAddress      => $client_mac_address,
432                 gotoXMonitor    => "",
433                 gotoXDriver     => "",
434                 gotoXMouseType  => "",
435                 gotoXMouseport  => "",
436                 gotoXkbModel    => "",
437                 gotoXHsync      => "",
438                 gotoXVsync      => "",
439                 gotoXResolution => "",
440                 ghUsbSupport    => "",
441                 gotoSndModule   => "",
442                 ghGfxAdapter    => "",
443                 ghNetNic        => "",
444                 ghSoundAdapter  => "",
445                 ghMemSize       => "",
446                 ghCpuType       => "",
447                 gotoModules     => [],
448                 ghIdeDev        => [],
449                 ghScsiDev       => [],
450         };
452         &main::daemon_log("Starting hardware detection", 4);
453         my $gfxcard= `$hwinfo --gfxcard`;
454         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
455         if(defined($primary_adapter)) {
456                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
457                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
458         }
459         my $monitor= `$hwinfo --monitor`;
460         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
461         if(defined($primary_monitor)) {
462                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
463                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
464         }
466         if(length($result->{gotoXHsync}) == 0) {
467                 # set default values
468                 $result->{gotoXHsync} = "30+50";
469                 $result->{gotoXVsync} = "30+90";
470         }
472         my $mouse= `$hwinfo --mouse`;
473         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
474         if(defined($primary_mouse)) {
475                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
476                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
477         }
479         my $sound= `$hwinfo --sound`;
480         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
481         if(defined($primary_sound)) {
482                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
483                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
484         }
486         my $netcard= `hwinfo --netcard`;
487         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
488         if(defined($primary_netcard)) {
489                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
490         }
492         my $keyboard= `hwinfo --keyboard`;
493         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
494         if(defined($primary_keyboard)) {
495                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
496         }
498         $result->{ghCpuType}= sprintf "%s / %s - %s", 
499         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
500         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
502         my @gotoModules=();
503         for my $line(`lsmod`) {
504                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
505                         next;
506                 } else {
507                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
508                 }
509         }
510         my %seen = ();
511         
512         # Remove duplicates and save
513         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
515         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
516         
517         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
518                 push @{$result->{ghIdeDev}}, $device;
519         }
521         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
522                 push @{$result->{ghScsiDev}}, $device;
523         }
525         &main::daemon_log("Hardware detection done!", 4);
527     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
528    
529     return &main::send_msg_hash_to_target(
530                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
531                 $main::server_address, 
532                 $main::server_key,
533         );
537 sub ping {
538     my ($msg, $msg_hash) = @_ ;
539     my $header = @{$msg_hash->{'header'}}[0];
540     my $source = @{$msg_hash->{'source'}}[0];
541     my $target = @{$msg_hash->{'target'}}[0];
542     my $session_id = @{$msg_hash->{'session_id'}}[0];
543     my $out_msg;
544     my $out_hash;
546     # there is no session_id so send 'got_new_ping'-msg
547     if (not defined $session_id) {
548         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
550     # there is a session_id so send 'answer_$session_id'-msg because there is 
551     # a process waiting for this message
552     } else {
553         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
554         &add_content2xml_hash($out_hash, "session_id", $session_id);
555     }
557     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
558     if (defined $forward_to_gosa) {
559         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
560     }
561     $out_msg = &main::create_xml_string($out_hash);
562     return $out_msg;
566 1;