Code

e0d5ed5ccef15412d1407545e28eba65a9d324e5
[gosa.git] / gosa-si / client / events / corefunctions.pm
1 package corefunctions;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = qw(get_events registered set_activated_for_installation new_ldap_config new_key generate_hw_digest detect_hardware reboot halt reinstall softupdate confirm_new_key);
5 @EXPORT = @events;
7 use strict;
8 use warnings;
9 use Data::Dumper;
10 use Fcntl;
11 use GOSA::GosaSupportDaemon;
12 use File::Basename;
15 my ($ldap_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
18 my %cfg_defaults = (
19     "client" => {
20         "ldap" => [\$ldap_enabled, 1],
21         "ldap_config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
22         "pam_config" => [\$pam_config, "/etc/pam_ldap.conf"],
23         "nss_config" => [\$nss_config, "/etc/libnss_ldap.conf"],
24         "fai_logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
25     },
26 );
28 BEGIN {}
30 END {}
32 ### Start ######################################################################
34 &main::read_configfile($main::cfg_file, %cfg_defaults);
37 my $server_address = $main::server_address;
38 my $server_key = $main::server_key;
39 my $client_mac_address = $main::client_mac_address;
41 sub write_to_file {
42     my ($string, $file) = @_;
43     my $error = 0;
45     if( not defined $file || not -f $file ) {
46         &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
47         $error++;
48     }
49     if( not defined $string || 0 == length($string)) {
50         &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
51         $error++;
52     }
53     
54     if( $error == 0 ) {
56         chomp($string);
57     
58         open(FILE, ">> $file");
59         print FILE $string."\n";
60         close(FILE);
61     }
63     return;    
64 }
67 sub get_events {
68     return \@events;
69 }
71 sub daemon_log {
72     my ($msg, $level) = @_ ;
73     &main::daemon_log($msg, $level);
74     return;
75 }
78 sub reboot {
79     my ($msg, $msg_hash) = @_ ;
80     &main::daemon_log("got reboot-msg: $msg", 5);
81     return;
82 }
85 sub halt {
86     my ($msg, $msg_hash) = @_ ;
88     &main::daemon_log("got halt-msg: $msg", 5);
89     return;
90 }
93 sub reinstall {
94     my ($msg, $msg_hash) = @_ ;
95     &main::daemon_log("got reinstall-msg: $msg", 5);
97     return;
98 }
101 sub softupdate {
102     my ($msg, $msg_hash) = @_ ;
103     &main::daemon_log("got softupdate-msg: $msg", 5);
105     return;
109 sub registered {
110     my ($msg, $msg_hash) = @_ ;
112     my $header = @{$msg_hash->{'header'}}[0];
113     if( $header eq "registered" ) {
114         my $source = @{$msg_hash->{'source'}}[0];
115         &main::daemon_log("registration at $source",1);
116     }
118     # set globaly variable client_address
119     my $target =  @{$msg_hash->{'target'}}[0];
120     $main::client_address = $target;
122     # set registration_flag to true 
123     my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
124     my $out_msg = &create_xml_string($out_hash);
125     return $out_msg;
130 sub set_activated_for_installation {
131     my ($msg, $msg_hash) = @_ ;
132     my $header = @{$msg_hash->{'header'}}[0];
133     my $target = @{$msg_hash->{'target'}}[0];
134     my $source = @{$msg_hash->{'source'}}[0];
136     my $Datei = "/var/run/gosa-si.activated";
137     open(DATEI, ">$Datei");
138     print DATEI "activated\n";
139     close DATEI;
140     
141     return;
145 sub server_leaving {
146     my ($msg_hash) = @_ ;
147     my $source = @{$msg_hash->{'source'}}[0]; 
148     my $header = @{$msg_hash->{'header'}}[0];
149     
150     daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
151     $main::server_address = "none";
152     $main::server_key = "none";
154     # reinitialization of default values in config file
155     &main::read_configfile;
156     
157     # registrated at new daemon
158     &main::register_at_server();
159        
160     return;   
164 sub new_ldap_config {
165         my ($msg, $msg_hash) = @_ ;
167         if( $ldap_enabled != 1 ) {
168                 return;
169         }
171         my $element;
172         my @ldap_uris;
173         my $ldap_base;
174         my @ldap_options;
175         my @pam_options;
176         my @nss_options;
177         my $goto_admin;
178         my $goto_secret;
179         my $admin_base= "";
180         my $department= "";
181         my $unit_tag;
183         # Transform input into array
184         while ( my ($key, $value) = each(%$msg_hash) ) {
185                 if ($key =~ /^(source|target|header)$/) {
186                         next;
187                 }
189                 foreach $element (@$value) {
190                         if ($key =~ /^ldap_uri$/) {
191                                 push (@ldap_uris, $element);
192                                 next;
193                         }
194                         if ($key =~ /^ldap_base$/) {
195                                 $ldap_base= $element;
196                                 next;
197                         }
198                         if ($key =~ /^goto_admin$/) {
199                                 $goto_admin= $element;
200                                 next;
201                         }
202                         if ($key =~ /^goto_secret$/) {
203                                 $goto_secret= $element;
204                                 next;
205                         }
206                         if ($key =~ /^ldap_cfg$/) {
207                                 push (@ldap_options, "$element");
208                                 next;
209                         }
210                         if ($key =~ /^pam_cfg$/) {
211                                 push (@pam_options, "$element");
212                                 next;
213                         }
214                         if ($key =~ /^nss_cfg$/) {
215                                 push (@nss_options, "$element");
216                                 next;
217                         }
218                         if ($key =~ /^admin_base$/) {
219                                 $admin_base= $element;
220                                 next;
221                         }
222                         if ($key =~ /^department$/) {
223                                 $department= $element;
224                                 next;
225                         }
226                         if ($key =~ /^unit_tag$/) {
227                                 $unit_tag= $element;
228                                 next;
229                         }
230                 }
231         }
233         # Unit tagging enabled?
234         if (defined $unit_tag){
235                 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
236                 push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
237                 push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
238         }
240         # Sanity check
241         if(! ref($ldap_base) eq '') {
242                 daemon_log("Received empty LDAP config. Don't write anything.", 1);
243                 return;
244         }
246         # Setup ldap.conf
247         my $file1;
248         my $file2;
249         open(file1, "> $ldap_config");
250         print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
251         print file1 "URI";
252         foreach $element (@ldap_uris) {
253                 print file1 " $element";
254         }
255         print file1 "\nBASE $ldap_base\n";
256         foreach $element (@ldap_options) {
257                 print file1 "$element\n";
258         }
259         close (file1);
260         daemon_log("wrote $ldap_config", 5);
262         # Setup pam_ldap.conf / libnss_ldap.conf
263         open(file1, "> $pam_config");
264         open(file2, "> $nss_config");
265         print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
266         print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
267         print file1 "uri";
268         print file2 "uri";
269         foreach $element (@ldap_uris) {
270                 print file1 " $element";
271                 print file2 " $element";
272         }
273         print file1 "\nbase $ldap_base\n";
274         print file2 "\nbase $ldap_base\n";
275         foreach $element (@pam_options) {
276                 print file1 "$element\n";
277         }
278         foreach $element (@nss_options) {
279                 print file2 "$element\n";
280         }
281         close (file2);
282         daemon_log("wrote $nss_config", 5);
283         close (file1);
284         daemon_log("wrote $pam_config", 5);
286         # Create goto.secrets if told so - for compatibility reasons
287         if (defined $goto_admin){
288                 open(file1, "> /etc/goto/secret");
289                 close(file1);
290                 chown(0,0, "/etc/goto/secret");
291                 chmod(0600, "/etc/goto/secret");
292                 open(file1, "> /etc/goto/secret");
293                 print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
294                 close(file1);
295                 daemon_log("wrote /etc/goto/secret", 5);
296         }
300         # Write shell based config
301         my $cfg_name= dirname($ldap_config)."/ldap-shell.conf";
302         open(file1, "> $cfg_name");
303         print file1 "LDAP_BASE=\"$ldap_base\"\n";
304         print file1 "ADMIN_BASE=\"$admin_base\"\n";
305         print file1 "DEPARTMENT=\"$department\"\n";
306         print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
307         print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
308         close(file1);
309         daemon_log("wrote $cfg_name", 5);
311         return;
315 sub new_key {
316     # my ($msg_hash) = @_ ;
317     my $new_server_key = &main::create_passwd();
319     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
320     my $out_msg = &create_xml_string($out_hash);
321     return $out_msg; 
325 sub confirm_new_key {
326     my ($msg, $msg_hash) = @_ ;
327     my $header = @{$msg_hash->{'header'}}[0];
328     my $target = @{$msg_hash->{'target'}}[0];
329     my $source = @{$msg_hash->{'source'}}[0];
331     &main::daemon_log("confirm new key from $source", 5);
332     return;
337 sub detect_hardware {
340     &write_to_file('goto-hardware-detection-start', $fai_logpath);
342         my $hwinfo= `which hwinfo`;
343         chomp $hwinfo;
345         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
346                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
347                 return;
348         }
350         my $result= {
351                 gotoHardwareChecksum => &main::generate_hw_digest(),
352                 macAddress      => $client_mac_address,
353                 gotoXMonitor    => "",
354                 gotoXDriver     => "",
355                 gotoXMouseType  => "",
356                 gotoXMouseport  => "",
357                 gotoXkbModel    => "",
358                 gotoXHsync      => "",
359                 gotoXVsync      => "",
360                 gotoXResolution => "",
361                 ghUsbSupport    => "",
362                 gotoSndModule   => "",
363                 ghGfxAdapter    => "",
364                 ghNetNic        => "",
365                 ghSoundAdapter  => "",
366                 ghMemSize       => "",
367                 ghCpuType       => "",
368                 gotoModules     => [],
369                 ghIdeDev        => [],
370                 ghScsiDev       => [],
371         };
373         &main::daemon_log("Starting hardware detection", 4);
374         my $gfxcard= `$hwinfo --gfxcard`;
375         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
376         if(defined($primary_adapter)) {
377                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
378                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
379         }
380         my $monitor= `$hwinfo --monitor`;
381         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
382         if(defined($primary_monitor)) {
383                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
384                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
385         }
387         if(length($result->{gotoXHsync}) == 0) {
388                 # set default values
389                 $result->{gotoXHsync} = "30+50";
390                 $result->{gotoXVsync} = "30+90";
391         }
393         my $mouse= `$hwinfo --mouse`;
394         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
395         if(defined($primary_mouse)) {
396                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
397                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
398         }
400         my $sound= `$hwinfo --sound`;
401         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
402         if(defined($primary_sound)) {
403                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
404                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
405         }
407         my $netcard= `hwinfo --netcard`;
408         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
409         if(defined($primary_netcard)) {
410                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
411         }
413         my $keyboard= `hwinfo --keyboard`;
414         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
415         if(defined($primary_keyboard)) {
416                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
417         }
419         $result->{ghCpuType}= sprintf "%s / %s - %s", 
420         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
421         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
423         my @gotoModules=();
424         for my $line(`lsmod`) {
425                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
426                         next;
427                 } else {
428                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
429                 }
430         }
431         my %seen = ();
432         
433         # Remove duplicates and save
434         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
436         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
437         
438         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
439                 push @{$result->{ghIdeDev}}, $device;
440         }
442         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
443                 push @{$result->{ghScsiDev}}, $device;
444         }
446         &main::daemon_log("Hardware detection done!", 4);
448     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
449    
450     return &main::send_msg_hash_to_target(
451                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
452                 $main::server_address, 
453                 $main::server_key,
454         );
458 sub ping {
459     my ($msg, $msg_hash) = @_ ;
460     my $header = @{$msg_hash->{'header'}}[0];
461     my $source = @{$msg_hash->{'source'}}[0];
462     my $target = @{$msg_hash->{'target'}}[0];
463    
464     # switch target and source and send msg back
465     my $out_hash = &main::create_xml_hash("got_ping", $target, $source);
466     my $out_msg = &main::create_xml_string($out_hash);
467     return $out_msg;
472 1;