Code

Created trunk inside of 2.6-lhm
[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     open($opts_file_FH, ">$main::opts_file");
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     return;
451 sub new_key {
452     # my ($msg_hash) = @_ ;
453     my $new_server_key = &main::create_passwd();
455     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
456     my $out_msg = &create_xml_string($out_hash);
458     # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
459     # comes from gosa-si-server within a given time
460     
462     return $out_msg; 
466 sub confirm_new_key {
467     my ($msg, $msg_hash) = @_ ;
468     my $header = @{$msg_hash->{'header'}}[0];
469     my $target = @{$msg_hash->{'target'}}[0];
470     my $source = @{$msg_hash->{'source'}}[0];
472     &main::daemon_log("confirm new key from $source", 5);
473     return;
478 sub detect_hardware {
480     &write_to_file('goto-hardware-detection-start', $fai_logpath);
482         my $hwinfo= `which hwinfo`;
483         chomp $hwinfo;
485         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
486                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
487                 return;
488         }
490         my $result= {
491                 gotoHardwareChecksum => &main::generate_hw_digest(),
492                 macAddress      => $client_mac_address,
493                 gotoXMonitor    => "",
494                 gotoXDriver     => "",
495                 gotoXMouseType  => "",
496                 gotoXMouseport  => "",
497                 gotoXkbModel    => "",
498                 gotoXHsync      => "",
499                 gotoXVsync      => "",
500                 gotoXResolution => "",
501                 ghUsbSupport    => "",
502                 gotoSndModule   => "",
503                 ghGfxAdapter    => "",
504                 ghNetNic        => "",
505                 ghSoundAdapter  => "",
506                 ghMemSize       => "",
507                 ghCpuType       => "",
508                 gotoModules     => [],
509                 ghIdeDev        => [],
510                 ghScsiDev       => [],
511         };
513         &main::daemon_log("Starting hardware detection", 4);
514         my $gfxcard= `$hwinfo --gfxcard`;
515         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
516         if(defined($primary_adapter)) {
517                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
518                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
519         }
520         my $monitor= `$hwinfo --monitor`;
521         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
522         if(defined($primary_monitor)) {
523                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
524                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
525         }
527         if(length($result->{gotoXHsync}) == 0) {
528                 # set default values
529                 $result->{gotoXHsync} = "30+50";
530                 $result->{gotoXVsync} = "30+90";
531         }
533         my $mouse= `$hwinfo --mouse`;
534         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
535         if(defined($primary_mouse)) {
536                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
537                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
538         }
540         my $sound= `$hwinfo --sound`;
541         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
542         if(defined($primary_sound)) {
543                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
544                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
545         }
547         my $netcard= `hwinfo --netcard`;
548         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
549         if(defined($primary_netcard)) {
550                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
551         }
553         my $keyboard= `hwinfo --keyboard`;
554         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
555         if(defined($primary_keyboard)) {
556                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
557         }
559         $result->{ghCpuType}= sprintf "%s / %s - %s", 
560         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
561         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
563         my @gotoModules=();
564         for my $line(`lsmod`) {
565                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
566                         next;
567                 } else {
568                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
569                 }
570         }
571         my %seen = ();
572         
573         # Remove duplicates and save
574         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
576         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
577         
578         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
579                 push @{$result->{ghIdeDev}}, $device;
580         }
582         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
583                 push @{$result->{ghScsiDev}}, $device;
584         }
586         &main::daemon_log("Hardware detection done!", 4);
588     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
589    
590     return &main::send_msg_hash_to_target(
591                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
592                 $main::server_address, 
593                 $main::server_key,
594         );
598 sub ping {
599     my ($msg, $msg_hash) = @_ ;
600     my $header = @{$msg_hash->{'header'}}[0];
601     my $source = @{$msg_hash->{'source'}}[0];
602     my $target = @{$msg_hash->{'target'}}[0];
603     my $session_id = @{$msg_hash->{'session_id'}}[0];
604     my $out_msg;
605     my $out_hash;
607     # there is no session_id so send 'got_new_ping'-msg
608     if (not defined $session_id) {
609         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
611     # there is a session_id so send 'answer_$session_id'-msg because there is 
612     # a process waiting for this message
613     } else {
614         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
615         &add_content2xml_hash($out_hash, "session_id", $session_id);
616     }
618     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
619     if (defined $forward_to_gosa) {
620         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
621     }
622     $out_msg = &main::create_xml_string($out_hash);
623     return $out_msg;
627 1;