Code

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