f1a8935cc128c6c4bc9ef4bda18c42e159e2b905
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 Fcntl;
22 use GOSA::GosaSupportDaemon;
23 use File::Basename;
25 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath, $ldap_config_exit_hook);
27 my $chrony_file = "/etc/chrony/chrony.conf";
28 my $syslog_file = "/etc/syslog.conf";
30 my %cfg_defaults = (
31 "client" => {
32 "ldap" => [\$ldap_enabled, 1],
33 "offline-ldap" => [\$offline_enabled, 0],
34 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
35 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
36 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
37 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
38 "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
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 }
68 if( $error == 0 ) {
70 chomp($string);
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 &main::_setREGISTERED(1);
112 # Write the MAC address to file
113 if(stat($main::opts_file)) {
114 unlink($main::opts_file);
115 }
117 my $opts_file_FH;
118 my $hostname= $main::client_dnsname;
119 $hostname =~ s/\..*$//;
120 $hostname =~ tr/A-Z/a-z/;
121 sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
122 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
123 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
124 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
125 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
126 if(defined(@{$msg_hash->{'ldap_available'}}) &&
127 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
128 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
129 }
130 if(defined(@{$msg_hash->{'error'}})) {
131 my $errormsg= @{$msg_hash->{'error'}}[0];
132 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
133 &write_to_file($errormsg, $fai_logpath);
134 }
135 close($opts_file_FH);
137 return;
138 }
140 sub server_leaving {
141 my ($msg_hash) = @_ ;
142 my $source = @{$msg_hash->{'source'}}[0];
143 my $header = @{$msg_hash->{'header'}}[0];
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;
152 # registrated at new daemon
153 &main::register_at_server();
155 return;
156 }
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;
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 }
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 }
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 }
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;
212 }
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;
274 }
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("INFO: 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("INFO: Wrote $nss_config", 5);
395 close (file1);
396 daemon_log("INFO: 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("INFO: 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("INFO: 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("INFO: Wrote $cfg_name", 5);
445 }
447 # Set permissions and ownership structure of
448 chown(0, 0, $cfg_name);
449 chmod(0644, $cfg_name);
451 # Allow custom scripts to be executed
452 if (defined $ldap_config_exit_hook) {
453 system($ldap_config_exit_hook);
454 daemon_log("executed hook $ldap_config_exit_hook", 5);
455 }
457 return;
458 }
461 sub new_key {
462 # Create new key
463 my $new_server_key = &main::create_passwd();
465 # Send new_key message to server
466 my $errSend = &main::send_msg_hash_to_target(
467 &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
468 $main::server_address,
469 $main::server_key,
470 );
472 # Set global key
473 if (not $errSend) {
474 $main::server_key = $new_server_key;
475 }
477 return;
478 }
481 sub confirm_new_key {
482 my ($msg, $msg_hash) = @_ ;
483 my $source = @{$msg_hash->{'source'}}[0];
485 &main::daemon_log("confirm new key from $source", 5);
486 return;
488 }
491 sub detect_hardware {
493 &write_to_file('goto-hardware-detection-start', $fai_logpath);
495 my $hwinfo= `which hwinfo`;
496 chomp $hwinfo;
498 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
499 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
500 return;
501 }
503 my $result= {
504 gotoHardwareChecksum => &main::generate_hw_digest(),
505 macAddress => $client_mac_address,
506 gotoXMonitor => "",
507 gotoXDriver => "",
508 gotoXMouseType => "",
509 gotoXMouseport => "",
510 gotoXkbModel => "",
511 gotoXHsync => "",
512 gotoXVsync => "",
513 gotoXResolution => "",
514 ghUsbSupport => "",
515 gotoSndModule => "",
516 ghGfxAdapter => "",
517 ghNetNic => "",
518 ghSoundAdapter => "",
519 ghMemSize => "",
520 ghCpuType => "",
521 gotoModules => [],
522 ghIdeDev => [],
523 ghScsiDev => [],
524 };
526 &main::daemon_log("Starting hardware detection", 4);
527 my $gfxcard= `$hwinfo --gfxcard`;
528 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
529 if(defined($primary_adapter)) {
530 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
531 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
532 }
533 my $monitor= `$hwinfo --monitor`;
534 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
535 if(defined($primary_monitor)) {
536 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
537 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
538 }
540 if(length($result->{gotoXHsync}) == 0) {
541 # set default values
542 $result->{gotoXHsync} = "30+50";
543 $result->{gotoXVsync} = "30+90";
544 }
546 my $mouse= `$hwinfo --mouse`;
547 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
548 if(defined($primary_mouse)) {
549 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
550 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
551 }
553 my $sound= `$hwinfo --sound`;
554 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
555 if(defined($primary_sound)) {
556 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
557 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
558 }
560 my $netcard= `hwinfo --netcard`;
561 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
562 if(defined($primary_netcard)) {
563 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
564 }
566 my $keyboard= `hwinfo --keyboard`;
567 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
568 if(defined($primary_keyboard)) {
569 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
570 }
572 $result->{ghCpuType}= sprintf "%s / %s - %s",
573 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
574 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
576 my @gotoModules=();
577 for my $line(`lsmod`) {
578 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
579 next;
580 } else {
581 push @gotoModules, $1 if $line =~ /^(\w*).*$/
582 }
583 }
584 my %seen = ();
586 # Remove duplicates and save
587 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
589 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
591 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
592 push @{$result->{ghIdeDev}}, $device;
593 }
595 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
596 push @{$result->{ghScsiDev}}, $device;
597 }
599 &main::daemon_log("Hardware detection done!", 4);
601 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
603 &main::send_msg_hash_to_target(
604 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
605 $main::server_address,
606 $main::server_key,
607 );
609 return;
610 }
613 sub ping {
614 my ($msg, $msg_hash) = @_ ;
615 my $header = @{$msg_hash->{'header'}}[0];
616 my $source = @{$msg_hash->{'source'}}[0];
617 my $target = @{$msg_hash->{'target'}}[0];
618 my $session_id = @{$msg_hash->{'session_id'}}[0];
619 my $out_msg;
620 my $out_hash;
622 # there is no session_id so send 'got_new_ping'-msg
623 if (not defined $session_id) {
624 $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
626 # there is a session_id so send 'answer_$session_id'-msg because there is
627 # a process waiting for this message
628 } else {
629 $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
630 &add_content2xml_hash($out_hash, "session_id", $session_id);
631 }
633 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
634 if (defined $forward_to_gosa) {
635 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
636 }
637 $out_msg = &main::create_xml_string($out_hash);
638 return $out_msg;
640 }
642 1;