1 package corefunctions;
3 use strict;
4 use warnings;
5 use Exporter;
6 use Fcntl;
7 use GOSA::GosaSupportDaemon;
8 use File::Basename;
10 @ISA = qw(Exporter);
11 my @events = (
12 "get_events",
13 "registered",
14 "new_syslog_config",
15 "new_ntp_config",
16 "new_ldap_config",
17 "new_key",
18 "generate_hw_digest", # no implementations
19 "detect_hardware",
20 "confirm_new_key",
21 "ping",
22 "import_events", # no implementations
23 );
24 @EXPORT = @events;
26 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath, $ldap_config_exit_hook);
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 "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
40 },
41 );
43 BEGIN {}
45 END {}
47 ### Start ######################################################################
49 &main::read_configfile($main::cfg_file, %cfg_defaults);
52 my $server_address = $main::server_address;
53 my $server_key = $main::server_key;
54 my $client_mac_address = $main::client_mac_address;
56 sub write_to_file {
57 my ($string, $file) = @_;
58 my $error = 0;
60 if( not defined $file || not -f $file ) {
61 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
62 $error++;
63 }
64 if( not defined $string || 0 == length($string)) {
65 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
66 $error++;
67 }
69 if( $error == 0 ) {
71 chomp($string);
73 if( not -f $file ) {
74 open (my $FD_FILE, "$file");
75 close($FD_FILE);
76 }
77 open(my $FD_FILE, ">>", "$file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
78 print $FD_FILE $string."\n";
79 close($FD_FILE);
80 }
82 return;
83 }
86 sub get_events {
87 return \@events;
88 }
90 sub daemon_log {
91 my ($msg, $level) = @_ ;
92 &main::daemon_log($msg, $level);
93 return;
94 }
96 sub registered {
97 my ($msg, $msg_hash) = @_ ;
99 my $header = @{$msg_hash->{'header'}}[0];
100 if( $header eq "registered" ) {
101 my $source = @{$msg_hash->{'source'}}[0];
102 &main::daemon_log("INFO: registration at $source", 1);
103 $main::server_address = $source;
104 }
106 # set globaly variable client_address
107 my $target = @{$msg_hash->{'target'}}[0];
108 $main::client_address = $target;
110 # set registration_flag to true
111 &main::_setREGISTERED(1);
113 # Write the MAC address to file
114 if(stat($main::opts_file)) {
115 unlink($main::opts_file);
116 }
118 my $opts_file_FH;
119 my $hostname= $main::client_dnsname;
120 $hostname =~ s/\..*$//;
121 $hostname =~ tr/A-Z/a-z/;
122 sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
123 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
124 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
125 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
126 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
127 if(defined(@{$msg_hash->{'ldap_available'}}) &&
128 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
129 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
130 }
131 if(defined(@{$msg_hash->{'error'}})) {
132 my $errormsg= @{$msg_hash->{'error'}}[0];
133 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
134 &write_to_file($errormsg, $fai_logpath);
135 }
136 close($opts_file_FH);
138 return;
139 }
141 sub server_leaving {
142 my ($msg_hash) = @_ ;
143 my $source = @{$msg_hash->{'source'}}[0];
144 my $header = @{$msg_hash->{'header'}}[0];
146 daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
147 $main::server_address = "none";
148 $main::server_key = "none";
150 # reinitialization of default values in config file
151 &main::read_configfile;
153 # registrated at new daemon
154 &main::register_at_server();
156 return;
157 }
160 ## @method new_syslog_config
161 # Update or add syslog messages forwarding to specified syslog server.
162 # @param msg - STRING - xml message with tag server
163 # @param msg_hash - HASHREF - message information parsed into a hash
164 sub new_syslog_config {
165 my ($msg, $msg_hash) = @_ ;
167 # Sanity check of incoming message
168 if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
169 &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
170 return;
171 }
173 # Fetch the new syslog server from incoming message
174 my $syslog_server = @{$msg_hash->{'server'}}[0];
175 &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5);
176 my $found_server_flag = 0;
178 # Sanity check of /etc/syslog.conf
179 if (not -f $syslog_file) {
180 &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
181 return;
182 }
184 # Substitute existing server with new syslog server
185 open (my $FILE, "<","$syslog_file");
186 my @file = <$FILE>;
187 close($FILE);
188 my $syslog_server_line = "*.*\t@".$syslog_server."\n";
189 foreach my $line (@file) {
190 if ($line =~ /^\*\.\*\s+@/) {
191 $line = $syslog_server_line;
192 $found_server_flag++;
193 }
194 }
196 # Append new server if no old server configuration found
197 if (not $found_server_flag) {
198 push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
199 push(@file, $syslog_server_line);
200 }
202 # Write changes to file and close it
203 open (my $FILE, "+>","$syslog_file");
204 print $FILE join("", @file);
205 close($FILE);
206 &main::daemon_log("INFO: Wrote new configuration file: $syslog_file", 5);
208 # Restart syslog deamon
209 my $res = qx(/etc/init.d/sysklogd restart);
210 &main::daemon_log("INFO: restart syslog daemon: $res", 5);
212 return;
213 }
216 ## @method new_ntp_config
217 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
218 # @param msg - STRING - xml message with tag server
219 # @param msg_hash - HASHREF - message information parsed into a hash
220 sub new_ntp_config {
221 my ($msg, $msg_hash) = @_ ;
223 # Sanity check of incoming message
224 if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
225 &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
226 return;
227 }
229 # Fetch the new ntp server from incoming message
230 my $ntp_servers = $msg_hash->{'server'};
231 &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5);
232 my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
233 my $found_server_flag = 0;
235 # Sanity check of /etc/chrony/chrony.conf
236 if (not -f $chrony_file) {
237 &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
238 return;
239 }
241 # Substitute existing server with new ntp server
242 open (my $FILE, "<","$chrony_file");
243 my @file = <$FILE>;
244 close($FILE);
245 my @new_file;
246 foreach my $line (@file) {
247 if ($line =~ /^server\s+/) {
248 if ($found_server_flag) {
249 $line =~ s/^server\s+[\S]+\s+$//;
250 } else {
251 $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
252 }
253 $found_server_flag++;
254 }
255 push(@new_file, $line);
256 }
258 # Append new server if no old server configuration found
259 if (not $found_server_flag) {
260 push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
261 push(@new_file, $ntp_servers_string);
262 }
264 # Write changes to file and close it
265 open (my $FILE, ">","$chrony_file");
266 print $FILE join("", @new_file);
267 close($FILE);
268 &main::daemon_log("INFO: Wrote new configuration file: $chrony_file", 5);
270 # Restart chrony deamon
271 my $res = qx(/etc/init.d/chrony force-reload);
272 &main::daemon_log("INFO: restart chrony daemon: $res", 5);
274 return;
275 }
278 sub new_ldap_config {
279 my ($msg, $msg_hash) = @_ ;
281 if( $ldap_enabled != 1 ) {
282 return;
283 }
285 my $element;
286 my @ldap_uris;
287 my $ldap_base;
288 my @ldap_options;
289 my @pam_options;
290 my @nss_options;
291 my $goto_admin;
292 my $goto_secret;
293 my $admin_base= "";
294 my $department= "";
295 my $release= "";
296 my $unit_tag;
298 # Transform input into array
299 while ( my ($key, $value) = each(%$msg_hash) ) {
300 if ($key =~ /^(source|target|header)$/) {
301 next;
302 }
304 foreach $element (@$value) {
305 if ($key =~ /^ldap_uri$/) {
306 push (@ldap_uris, $element);
307 next;
308 }
309 if ($key =~ /^ldap_base$/) {
310 $ldap_base= $element;
311 next;
312 }
313 if ($key =~ /^goto_admin$/) {
314 $goto_admin= $element;
315 next;
316 }
317 if ($key =~ /^goto_secret$/) {
318 $goto_secret= $element;
319 next;
320 }
321 if ($key =~ /^ldap_cfg$/) {
322 push (@ldap_options, "$element");
323 next;
324 }
325 if ($key =~ /^pam_cfg$/) {
326 push (@pam_options, "$element");
327 next;
328 }
329 if ($key =~ /^nss_cfg$/) {
330 push (@nss_options, "$element");
331 next;
332 }
333 if ($key =~ /^admin_base$/) {
334 $admin_base= $element;
335 next;
336 }
337 if ($key =~ /^department$/) {
338 $department= $element;
339 next;
340 }
341 if ($key =~ /^unit_tag$/) {
342 $unit_tag= $element;
343 next;
344 }
345 if ($key =~ /^release$/) {
346 $release= $element;
347 next;
348 }
349 }
350 }
352 # Unit tagging enabled?
353 if (defined $unit_tag){
354 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
355 push (@nss_options, "nss_base_passwd $admin_base?sub?gosaUnitTag=$unit_tag");
356 push (@nss_options, "nss_base_group $admin_base?sub?gosaUnitTag=$unit_tag");
357 }
359 # Setup ldap.conf
360 my $file1;
361 my $file2;
362 open(my $file1, ">","$ldap_config");
363 print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
364 print $file1 "URI";
365 foreach $element (@ldap_uris) {
366 print $file1 " $element";
367 }
368 print $file1 "\nBASE $ldap_base\n";
369 foreach $element (@ldap_options) {
370 print $file1 "$element\n";
371 }
372 close ($file1);
373 daemon_log("INFO: Wrote $ldap_config", 5);
375 # Setup pam_ldap.conf / libnss-ldap.conf
376 open(my $file1, ">","$pam_config");
377 open(my $file2, ">","$nss_config");
378 print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
379 print $file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
380 print $file1 "uri";
381 print $file2 "uri";
382 foreach $element (@ldap_uris) {
383 print $file1 " $element";
384 print $file2 " $element";
385 }
386 print $file1 "\nbase $ldap_base\n";
387 print $file2 "\nbase $ldap_base\n";
388 foreach $element (@pam_options) {
389 print $file1 "$element\n";
390 }
391 foreach $element (@nss_options) {
392 print $file2 "$element\n";
393 }
394 close ($file2);
395 daemon_log("INFO: Wrote $nss_config", 5);
396 close ($file1);
397 daemon_log("INFO: Wrote $pam_config", 5);
399 # Create goto.secrets if told so - for compatibility reasons
400 if (defined $goto_admin){
401 open(my $file1, ">","/etc/goto/secret");
402 close($file1);
403 chown(0,0, "/etc/goto/secret");
404 chmod(0600, "/etc/goto/secret");
405 open(my $file1, ">","/etc/goto/secret");
406 print $file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
407 close($file1);
408 daemon_log("INFO: Wrote /etc/goto/secret", 5);
409 }
411 # Write shell based config
412 my $cfg_name= "/etc/ldap/ldap-shell.conf";
414 # Get first LDAP server
415 my $ldap_server= $ldap_uris[0];
416 $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
418 open(my $file1, ">","$cfg_name");
419 print $file1 "LDAP_BASE=\"$ldap_base\"\n";
420 print $file1 "LDAP_SERVER=\"$ldap_server\"\n";
421 print $file1 "LDAP_URIS=\"@ldap_uris\"\n";
422 print $file1 "ADMIN_BASE=\"$admin_base\"\n";
423 print $file1 "DEPARTMENT=\"$department\"\n";
424 print $file1 "RELEASE=\"$release\"\n";
425 print $file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
426 print $file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
427 close($file1);
428 daemon_log("INFO: Wrote $cfg_name", 5);
430 # Write offline config
431 if ($offline_enabled){
432 $cfg_name= "/etc/ldap/ldap-offline.conf";
434 # Get first LDAP server
435 open(my $file1, ">","$cfg_name");
436 print $file1 "LDAP_BASE=\"$ldap_base\"\n";
437 print $file1 "LDAP_SERVER=\"127.0.0.1\"\n";
438 print $file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
439 print $file1 "ADMIN_BASE=\"$admin_base\"\n";
440 print $file1 "DEPARTMENT=\"$department\"\n";
441 print $file1 "RELEASE=\"$release\"\n";
442 print $file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
443 print $file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
444 close($file1);
445 daemon_log("INFO: Wrote $cfg_name", 5);
446 }
448 # Set permissions and ownership structure of
449 chown(0, 0, $cfg_name);
450 chmod(0644, $cfg_name);
452 # Allow custom scripts to be executed
453 if (defined $ldap_config_exit_hook) {
454 system($ldap_config_exit_hook);
455 daemon_log("Hook $ldap_config_exit_hook returned with code ".($? >> 8), 5);
456 }
458 return;
459 }
462 sub new_key {
463 # Create new key
464 my $new_server_key = &main::create_passwd();
466 # Send new_key message to server
467 my $errSend = &main::send_msg_hash_to_target(
468 &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
469 $main::server_address,
470 $main::server_key,
471 );
473 # Set global key
474 if (not $errSend) {
475 $main::server_key = $new_server_key;
476 }
478 return;
479 }
482 sub confirm_new_key {
483 my ($msg, $msg_hash) = @_ ;
484 my $source = @{$msg_hash->{'source'}}[0];
486 &main::daemon_log("confirm new key from $source", 5);
487 return;
489 }
492 sub detect_hardware {
494 &write_to_file('goto-hardware-detection-start', $fai_logpath);
496 my $hwinfo= `which hwinfo`;
497 chomp $hwinfo;
499 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
500 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
501 return;
502 }
504 my $result= {
505 gotoHardwareChecksum => &main::generate_hw_digest(),
506 macAddress => $client_mac_address,
507 gotoXMonitor => "",
508 gotoXDriver => "",
509 gotoXMouseType => "",
510 gotoXMouseport => "",
511 gotoXkbModel => "",
512 gotoXHsync => "",
513 gotoXVsync => "",
514 gotoXResolution => "",
515 ghUsbSupport => "",
516 gotoSndModule => "",
517 ghGfxAdapter => "",
518 ghNetNic => "",
519 ghSoundAdapter => "",
520 ghMemSize => "",
521 ghCpuType => "",
522 gotoModules => [],
523 ghIdeDev => [],
524 ghScsiDev => [],
525 };
527 &main::daemon_log("Starting hardware detection", 4);
528 my $gfxcard= `$hwinfo --gfxcard`;
529 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
530 if(defined($primary_adapter)) {
531 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
532 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
533 }
534 my $monitor= `$hwinfo --monitor`;
535 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
536 if(defined($primary_monitor)) {
537 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
538 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
539 }
541 if(length($result->{gotoXHsync}) == 0) {
542 # set default values
543 $result->{gotoXHsync} = "30+50";
544 $result->{gotoXVsync} = "30+90";
545 }
547 my $mouse= `$hwinfo --mouse`;
548 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
549 if(defined($primary_mouse)) {
550 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
551 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
552 }
554 my $sound= `$hwinfo --sound`;
555 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
556 if(defined($primary_sound)) {
557 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
558 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
559 }
561 my $netcard= `hwinfo --netcard`;
562 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
563 if(defined($primary_netcard)) {
564 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
565 }
567 my $keyboard= `hwinfo --keyboard`;
568 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
569 if(defined($primary_keyboard)) {
570 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
571 }
573 $result->{ghCpuType}= sprintf "%s / %s - %s",
574 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
575 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
577 my @gotoModules=();
578 for my $line(`lsmod`) {
579 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
580 next;
581 } else {
582 push @gotoModules, $1 if $line =~ /^(\w*).*$/
583 }
584 }
585 my %seen = ();
587 # Remove duplicates and save
588 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
590 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
592 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
593 push @{$result->{ghIdeDev}}, $device;
594 }
596 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
597 push @{$result->{ghScsiDev}}, $device;
598 }
600 &main::daemon_log("Hardware detection done!", 4);
602 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
604 &main::send_msg_hash_to_target(
605 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
606 $main::server_address,
607 $main::server_key,
608 );
610 return;
611 }
614 sub ping {
615 my ($msg, $msg_hash) = @_ ;
616 my $header = @{$msg_hash->{'header'}}[0];
617 my $source = @{$msg_hash->{'source'}}[0];
618 my $target = @{$msg_hash->{'target'}}[0];
619 my $session_id = @{$msg_hash->{'session_id'}}[0];
620 my $out_msg;
621 my $out_hash;
623 # there is no session_id so send 'got_new_ping'-msg
624 if (not defined $session_id) {
625 $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
627 # there is a session_id so send 'answer_$session_id'-msg because there is
628 # a process waiting for this message
629 } else {
630 $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
631 &add_content2xml_hash($out_hash, "session_id", $session_id);
632 }
634 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
635 if (defined $forward_to_gosa) {
636 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
637 }
638 $out_msg = &main::create_xml_string($out_hash);
639 return $out_msg;
641 }
643 1;