1 package corefunctions;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "get_events",
6 "registered",
7 "new_ldap_config",
8 "new_key",
9 "generate_hw_digest", # no implementations
10 "detect_hardware",
11 "confirm_new_key",
12 "ping",
13 "new_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);
28 my %cfg_defaults = (
29 "client" => {
30 "ldap" => [\$ldap_enabled, 1],
31 "offline-ldap" => [\$offline_enabled, 0],
32 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
33 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
34 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
35 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
36 },
37 );
39 BEGIN {}
41 END {}
43 ### Start ######################################################################
45 &main::read_configfile($main::cfg_file, %cfg_defaults);
48 my $server_address = $main::server_address;
49 my $server_key = $main::server_key;
50 my $client_mac_address = $main::client_mac_address;
52 sub write_to_file {
53 my ($string, $file) = @_;
54 my $error = 0;
56 if( not defined $file || not -f $file ) {
57 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
58 $error++;
59 }
60 if( not defined $string || 0 == length($string)) {
61 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
62 $error++;
63 }
65 if( $error == 0 ) {
67 chomp($string);
69 if( not -f $file ) {
70 open (FILE, "$file");
71 close(FILE);
72 }
73 open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
74 print FILE $string."\n";
75 close(FILE);
76 }
78 return;
79 }
82 sub get_events {
83 return \@events;
84 }
86 sub daemon_log {
87 my ($msg, $level) = @_ ;
88 &main::daemon_log($msg, $level);
89 return;
90 }
92 sub registered {
93 my ($msg, $msg_hash) = @_ ;
95 my $header = @{$msg_hash->{'header'}}[0];
96 if( $header eq "registered" ) {
97 my $source = @{$msg_hash->{'source'}}[0];
98 &main::daemon_log("registration at $source",1);
99 $main::server_address = $source;
100 }
102 # set globaly variable client_address
103 my $target = @{$msg_hash->{'target'}}[0];
104 $main::client_address = $target;
106 # set registration_flag to true
107 my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
108 # Write the MAC address to file
109 if(stat($main::opts_file)) {
110 unlink($main::opts_file);
111 }
113 my $opts_file_FH;
114 my $hostname= $main::client_dnsname;
115 $hostname =~ s/\..*$//;
116 $hostname =~ tr/A-Z/a-z/;
117 open($opts_file_FH, ">$main::opts_file");
118 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
119 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
120 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
121 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
122 if(defined(@{$msg_hash->{'ldap_available'}}) &&
123 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
124 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
125 }
126 if(defined(@{$msg_hash->{'error'}})) {
127 my $errormsg= @{$msg_hash->{'error'}}[0];
128 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
129 &write_to_file($errormsg, $fai_logpath);
130 }
131 close($opts_file_FH);
133 my $out_msg = &create_xml_string($out_hash);
134 return $out_msg;
135 }
137 sub server_leaving {
138 my ($msg_hash) = @_ ;
139 my $source = @{$msg_hash->{'source'}}[0];
140 my $header = @{$msg_hash->{'header'}}[0];
142 daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
143 $main::server_address = "none";
144 $main::server_key = "none";
146 # reinitialization of default values in config file
147 &main::read_configfile;
149 # registrated at new daemon
150 &main::register_at_server();
152 return;
153 }
156 sub new_ldap_config {
157 my ($msg, $msg_hash) = @_ ;
159 if( $ldap_enabled != 1 ) {
160 return;
161 }
163 my $element;
164 my @ldap_uris;
165 my $ldap_base;
166 my @ldap_options;
167 my @pam_options;
168 my @nss_options;
169 my $goto_admin;
170 my $goto_secret;
171 my $admin_base= "";
172 my $department= "";
173 my $release= "";
174 my $unit_tag;
176 # Transform input into array
177 while ( my ($key, $value) = each(%$msg_hash) ) {
178 if ($key =~ /^(source|target|header)$/) {
179 next;
180 }
182 foreach $element (@$value) {
183 if ($key =~ /^ldap_uri$/) {
184 push (@ldap_uris, $element);
185 next;
186 }
187 if ($key =~ /^ldap_base$/) {
188 $ldap_base= $element;
189 next;
190 }
191 if ($key =~ /^goto_admin$/) {
192 $goto_admin= $element;
193 next;
194 }
195 if ($key =~ /^goto_secret$/) {
196 $goto_secret= $element;
197 next;
198 }
199 if ($key =~ /^ldap_cfg$/) {
200 push (@ldap_options, "$element");
201 next;
202 }
203 if ($key =~ /^pam_cfg$/) {
204 push (@pam_options, "$element");
205 next;
206 }
207 if ($key =~ /^nss_cfg$/) {
208 push (@nss_options, "$element");
209 next;
210 }
211 if ($key =~ /^admin_base$/) {
212 $admin_base= $element;
213 next;
214 }
215 if ($key =~ /^department$/) {
216 $department= $element;
217 next;
218 }
219 if ($key =~ /^unit_tag$/) {
220 $unit_tag= $element;
221 next;
222 }
223 if ($key =~ /^release$/) {
224 $release= $element;
225 next;
226 }
227 }
228 }
230 # Unit tagging enabled?
231 if (defined $unit_tag){
232 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
233 push (@nss_options, "nss_base_passwd $admin_base?sub?gosaUnitTag=$unit_tag");
234 push (@nss_options, "nss_base_group $admin_base?sub?gosaUnitTag=$unit_tag");
235 }
237 # Setup ldap.conf
238 my $file1;
239 my $file2;
240 open(file1, "> $ldap_config");
241 #print STDERR Dumper(PerlIO::get_layers(file1, output => 1));
242 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
243 print file1 "URI";
244 foreach $element (@ldap_uris) {
245 print file1 " $element";
246 }
247 print file1 "\nBASE $ldap_base\n";
248 foreach $element (@ldap_options) {
249 print file1 "$element\n";
250 }
251 close (file1);
252 daemon_log("wrote $ldap_config", 5);
254 # Setup pam_ldap.conf / libnss-ldap.conf
255 open(file1, "> $pam_config");
256 open(file2, "> $nss_config");
257 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
258 print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
259 print file1 "uri";
260 print file2 "uri";
261 foreach $element (@ldap_uris) {
262 print file1 " $element";
263 print file2 " $element";
264 }
265 print file1 "\nbase $ldap_base\n";
266 print file2 "\nbase $ldap_base\n";
267 foreach $element (@pam_options) {
268 print file1 "$element\n";
269 }
270 foreach $element (@nss_options) {
271 print file2 "$element\n";
272 }
273 close (file2);
274 daemon_log("wrote $nss_config", 5);
275 close (file1);
276 daemon_log("wrote $pam_config", 5);
278 # Create goto.secrets if told so - for compatibility reasons
279 if (defined $goto_admin){
280 open(file1, "> /etc/goto/secret");
281 close(file1);
282 chown(0,0, "/etc/goto/secret");
283 chmod(0600, "/etc/goto/secret");
284 open(file1, "> /etc/goto/secret");
285 print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
286 close(file1);
287 daemon_log("wrote /etc/goto/secret", 5);
288 }
290 # Write shell based config
291 my $cfg_name= "/etc/ldap/ldap-shell.conf";
293 # Get first LDAP server
294 my $ldap_server= $ldap_uris[0];
295 $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
297 open(file1, "> $cfg_name");
298 print file1 "LDAP_BASE=\"$ldap_base\"\n";
299 print file1 "LDAP_SERVER=\"$ldap_server\"\n";
300 print file1 "LDAP_URIS=\"@ldap_uris\"\n";
301 print file1 "ADMIN_BASE=\"$admin_base\"\n";
302 print file1 "DEPARTMENT=\"$department\"\n";
303 print file1 "RELEASE=\"$release\"\n";
304 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
305 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
306 close(file1);
307 daemon_log("wrote $cfg_name", 5);
309 # Write offline config
310 if ($offline_enabled){
311 $cfg_name= "/etc/ldap/ldap-offline.conf";
313 # Get first LDAP server
314 open(file1, "> $cfg_name");
315 print file1 "LDAP_BASE=\"$ldap_base\"\n";
316 print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
317 print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
318 print file1 "ADMIN_BASE=\"$admin_base\"\n";
319 print file1 "DEPARTMENT=\"$department\"\n";
320 print file1 "RELEASE=\"$release\"\n";
321 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
322 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
323 close(file1);
324 daemon_log("wrote $cfg_name", 5);
325 }
327 return;
328 }
331 sub new_key {
332 # my ($msg_hash) = @_ ;
333 my $new_server_key = &main::create_passwd();
335 my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);
336 my $out_msg = &create_xml_string($out_hash);
338 # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg
339 # comes from gosa-si-server within a given time
342 return $out_msg;
343 }
346 sub confirm_new_key {
347 my ($msg, $msg_hash) = @_ ;
348 my $header = @{$msg_hash->{'header'}}[0];
349 my $target = @{$msg_hash->{'target'}}[0];
350 my $source = @{$msg_hash->{'source'}}[0];
352 &main::daemon_log("confirm new key from $source", 5);
353 return;
355 }
358 sub detect_hardware {
360 &write_to_file('goto-hardware-detection-start', $fai_logpath);
362 my $hwinfo= `which hwinfo`;
363 chomp $hwinfo;
365 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
366 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
367 return;
368 }
370 my $result= {
371 gotoHardwareChecksum => &main::generate_hw_digest(),
372 macAddress => $client_mac_address,
373 gotoXMonitor => "",
374 gotoXDriver => "",
375 gotoXMouseType => "",
376 gotoXMouseport => "",
377 gotoXkbModel => "",
378 gotoXHsync => "",
379 gotoXVsync => "",
380 gotoXResolution => "",
381 ghUsbSupport => "",
382 gotoSndModule => "",
383 ghGfxAdapter => "",
384 ghNetNic => "",
385 ghSoundAdapter => "",
386 ghMemSize => "",
387 ghCpuType => "",
388 gotoModules => [],
389 ghIdeDev => [],
390 ghScsiDev => [],
391 };
393 &main::daemon_log("Starting hardware detection", 4);
394 my $gfxcard= `$hwinfo --gfxcard`;
395 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
396 if(defined($primary_adapter)) {
397 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
398 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
399 }
400 my $monitor= `$hwinfo --monitor`;
401 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
402 if(defined($primary_monitor)) {
403 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
404 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
405 }
407 if(length($result->{gotoXHsync}) == 0) {
408 # set default values
409 $result->{gotoXHsync} = "30+50";
410 $result->{gotoXVsync} = "30+90";
411 }
413 my $mouse= `$hwinfo --mouse`;
414 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
415 if(defined($primary_mouse)) {
416 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
417 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
418 }
420 my $sound= `$hwinfo --sound`;
421 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
422 if(defined($primary_sound)) {
423 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
424 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
425 }
427 my $netcard= `hwinfo --netcard`;
428 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
429 if(defined($primary_netcard)) {
430 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
431 }
433 my $keyboard= `hwinfo --keyboard`;
434 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
435 if(defined($primary_keyboard)) {
436 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
437 }
439 $result->{ghCpuType}= sprintf "%s / %s - %s",
440 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
441 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
443 my @gotoModules=();
444 for my $line(`lsmod`) {
445 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
446 next;
447 } else {
448 push @gotoModules, $1 if $line =~ /^(\w*).*$/
449 }
450 }
451 my %seen = ();
453 # Remove duplicates and save
454 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
456 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
458 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
459 push @{$result->{ghIdeDev}}, $device;
460 }
462 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
463 push @{$result->{ghScsiDev}}, $device;
464 }
466 &main::daemon_log("Hardware detection done!", 4);
468 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
470 return &main::send_msg_hash_to_target(
471 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
472 $main::server_address,
473 $main::server_key,
474 );
475 }
478 sub ping {
479 my ($msg, $msg_hash) = @_ ;
480 my $header = @{$msg_hash->{'header'}}[0];
481 my $source = @{$msg_hash->{'source'}}[0];
482 my $target = @{$msg_hash->{'target'}}[0];
483 my $session_id = @{$msg_hash->{'session_id'}}[0];
486 # switch target and source and send msg back
487 my $out_hash = &main::create_xml_hash("got_ping", $target, $source);
488 &add_content2xml_hash($out_hash, "session_id", $session_id);
489 my $out_msg = &main::create_xml_string($out_hash);
490 return $out_msg;
492 }
495 sub new_ping {
496 my ($msg, $msg_hash) = @_ ;
497 my $header = @{$msg_hash->{'header'}}[0];
498 my $source = @{$msg_hash->{'source'}}[0];
499 my $target = @{$msg_hash->{'target'}}[0];
500 my $session_id = @{$msg_hash->{'session_id'}}[0];
503 # switch target and source and send msg back
504 my $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
505 &add_content2xml_hash($out_hash, "session_id", $session_id);
506 my $out_msg = &main::create_xml_string($out_hash);
507 return $out_msg;
509 }
511 1;