1 package corefunctions;
3 use strict;
4 use warnings;
6 use File::Basename;
7 use GOsaSI::GosaSupportDaemon;
9 use Exporter;
10 use Fcntl;
13 our @ISA = qw(Exporter);
15 my @events = (
16 "get_events",
17 "registered",
18 "new_syslog_config",
19 "new_ntp_config",
20 "new_ldap_config",
21 "new_key",
22 "generate_hw_digest", # no implementations
23 "detect_hardware",
24 "confirm_new_key",
25 "ping",
26 "import_events", # no implementations
27 );
29 our @EXPORT = @events;
31 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath, $ldap_config_exit_hook);
33 my $chrony_file = "/etc/chrony/chrony.conf";
34 my $syslog_file = "/etc/syslog.conf";
36 # why is it re read here, the config is read at the start of the program no !!
37 my %cfg_defaults = (
38 "client" => {
39 "ldap" => [\$ldap_enabled, 1],
40 "offline-ldap" => [\$offline_enabled, 0],
41 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
42 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
43 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
44 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
45 "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
46 },
47 );
49 BEGIN {}
51 END {}
53 ### Start ######################################################################
55 # why not using the config read in the main ?? !!
56 &main::read_configfile($main::cfg_file, %cfg_defaults);
59 my $server_address = $main::server_address;
60 my $server_key = $main::server_key;
61 my $client_mac_address = $main::client_mac_address;
63 sub write_to_file {
64 my ($string, $file) = @_;
65 my $error = 0;
67 if( not defined $file || not -f $file ) {
68 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
69 $error++;
70 }
71 if( not defined $string || 0 == length($string)) {
72 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
73 $error++;
74 }
76 if( $error == 0 ) {
78 chomp($string);
80 if( not -f $file ) {
81 open (my $FD_FILE, "$file");
82 close($FD_FILE);
83 }
84 open(my $FD_FILE, ">>", "$file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
85 print $FD_FILE $string."\n";
86 close($FD_FILE);
87 }
89 return;
90 }
92 # should be the first function of each module of gosa-si !!
93 sub get_events {
94 return \@events;
95 }
97 sub daemon_log {
98 my ($msg, $level) = @_ ;
99 &main::daemon_log($msg, $level);
100 return;
101 }
103 sub registered {
104 my ($msg, $msg_hash) = @_ ;
106 my $header = @{$msg_hash->{'header'}}[0];
107 if( $header eq "registered" ) {
108 my $source = @{$msg_hash->{'source'}}[0];
109 &main::daemon_log("INFO: registration at $source", 1);
110 $main::server_address = $source;
111 }
113 # set globaly variable client_address
114 my $target = @{$msg_hash->{'target'}}[0];
115 $main::client_address = $target;
117 # set registration_flag to true
118 &main::_setREGISTERED(1);
120 # Write the MAC address to file
121 if(stat($main::opts_file)) {
122 unlink($main::opts_file);
123 }
125 my $opts_file_FH;
126 my $hostname= $main::client_dnsname;
127 $hostname =~ s/\..*$//;
128 $hostname =~ tr/A-Z/a-z/;
129 sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
130 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
131 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
132 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
133 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
134 if(defined(@{$msg_hash->{'ldap_available'}}) &&
135 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
136 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
137 }
138 if(defined(@{$msg_hash->{'error'}})) {
139 my $errormsg= @{$msg_hash->{'error'}}[0];
140 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
141 &write_to_file($errormsg, $fai_logpath);
142 }
143 close($opts_file_FH);
145 return;
146 }
148 sub server_leaving {
149 my ($msg_hash) = @_ ;
150 my $source = @{$msg_hash->{'source'}}[0];
151 my $header = @{$msg_hash->{'header'}}[0];
153 daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
154 $main::server_address = "none";
155 $main::server_key = "none";
157 # reinitialization of default values in config file
158 &main::read_configfile;
160 # registrated at new daemon
161 &main::register_at_server();
163 return;
164 }
167 ## @method new_syslog_config
168 # Update or add syslog messages forwarding to specified syslog server.
169 # @param msg - STRING - xml message with tag server
170 # @param msg_hash - HASHREF - message information parsed into a hash
171 sub new_syslog_config {
172 my ($msg, $msg_hash) = @_ ;
174 # Sanity check of incoming message
175 if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
176 &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
177 return;
178 }
180 # Fetch the new syslog server from incoming message
181 my $syslog_server = @{$msg_hash->{'server'}}[0];
182 &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5);
183 my $found_server_flag = 0;
185 # Sanity check of /etc/syslog.conf
186 if (not -f $syslog_file) {
187 &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
188 return;
189 }
191 # Substitute existing server with new syslog server
192 open (my $syslog, "<","$syslog_file");
193 my @file = <$syslog>;
194 close($syslog);
195 my $syslog_server_line = "*.*\t@".$syslog_server."\n";
196 foreach my $line (@file) {
197 if ($line =~ /^\*\.\*\s+@/) {
198 $line = $syslog_server_line;
199 $found_server_flag++;
200 }
201 }
203 # Append new server if no old server configuration found
204 if (not $found_server_flag) {
205 push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
206 push(@file, $syslog_server_line);
207 }
209 # Write changes to file and close it
210 open (my $new_syslog, "+>","$syslog_file");
211 print $new_syslog join("", @file);
212 close($new_syslog);
213 &main::daemon_log("INFO: Wrote new configuration file: $syslog_file", 5);
215 # Restart syslog deamon
216 my $res = qx(/etc/init.d/sysklogd restart);
217 &main::daemon_log("INFO: restart syslog daemon: $res", 5);
219 return;
220 }
223 ## @method new_ntp_config
224 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
225 # @param msg - STRING - xml message with tag server
226 # @param msg_hash - HASHREF - message information parsed into a hash
227 sub new_ntp_config {
228 my ($msg, $msg_hash) = @_ ;
230 # Sanity check of incoming message
231 if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
232 &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
233 return;
234 }
236 # Fetch the new ntp server from incoming message
237 my $ntp_servers = $msg_hash->{'server'};
238 &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5);
239 my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
240 my $found_server_flag = 0;
242 # Sanity check of /etc/chrony/chrony.conf
243 if (not -f $chrony_file) {
244 &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
245 return;
246 }
248 # Substitute existing server with new ntp server
249 open (my $ntp, "<","$chrony_file");
250 my @file = <$ntp>;
251 close($ntp);
252 my @new_file;
253 foreach my $line (@file) {
254 if ($line =~ /^server\s+/) {
255 if ($found_server_flag) {
256 $line =~ s/^server\s+[\S]+\s+$//;
257 } else {
258 $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
259 }
260 $found_server_flag++;
261 }
262 push(@new_file, $line);
263 }
265 # Append new server if no old server configuration found
266 if (not $found_server_flag) {
267 push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
268 push(@new_file, $ntp_servers_string);
269 }
271 # Write changes to file and close it
272 open (my $new_ntp, ">","$chrony_file");
273 print $new_ntp join("", @new_file);
274 close($new_ntp);
275 &main::daemon_log("INFO: Wrote new configuration file: $chrony_file", 5);
277 # Restart chrony deamon
278 my $res = qx(/etc/init.d/chrony force-reload);
279 &main::daemon_log("INFO: restart chrony daemon: $res", 5);
281 return;
282 }
285 sub new_ldap_config {
286 my ($msg, $msg_hash) = @_ ;
288 if( $ldap_enabled != 1 ) {
289 return;
290 }
292 my $element;
293 my @ldap_uris;
294 my $ldap_base;
295 my @ldap_options;
296 my @pam_options;
297 my @nss_options;
298 my $goto_admin;
299 my $goto_secret;
300 my $admin_base= "";
301 my $department= "";
302 my $release= "";
303 my $unit_tag;
304 my $ldap_file;
305 my $pam_file;
306 my $nss_file;
307 my $goto_file;
308 my $goto_secret_file;
309 my $ldap_offline_file;
310 my $ldap_shell_file;
312 my $ldap_shell_config = "/etc/ldap/ldap-shell.conf";
313 my $ldap_offline_config = "/etc/ldap/ldap-offline.conf";
314 my $goto_secret_config = "/etc/goto/secret";
316 # Transform input into array
317 while ( my ($key, $value) = each(%$msg_hash) ) {
318 if ($key =~ /^(source|target|header)$/) {
319 next;
320 }
322 foreach $element (@$value) {
323 if ($key =~ /^ldap_uri$/) {
324 push (@ldap_uris, $element);
325 next;
326 }
327 if ($key =~ /^ldap_base$/) {
328 $ldap_base= $element;
329 next;
330 }
331 if ($key =~ /^goto_admin$/) {
332 $goto_admin= $element;
333 next;
334 }
335 if ($key =~ /^goto_secret$/) {
336 $goto_secret= $element;
337 next;
338 }
339 if ($key =~ /^ldap_cfg$/) {
340 push (@ldap_options, "$element");
341 next;
342 }
343 if ($key =~ /^pam_cfg$/) {
344 push (@pam_options, "$element");
345 next;
346 }
347 if ($key =~ /^nss_cfg$/) {
348 push (@nss_options, "$element");
349 next;
350 }
351 if ($key =~ /^admin_base$/) {
352 $admin_base= $element;
353 next;
354 }
355 if ($key =~ /^department$/) {
356 $department= $element;
357 next;
358 }
359 if ($key =~ /^unit_tag$/) {
360 $unit_tag= $element;
361 next;
362 }
363 if ($key =~ /^release$/) {
364 $release= $element;
365 next;
366 }
367 }
368 }
370 # Unit tagging enabled?
371 if (defined $unit_tag){
372 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
373 push (@nss_options, "nss_base_passwd $admin_base?sub?gosaUnitTag=$unit_tag");
374 push (@nss_options, "nss_base_group $admin_base?sub?gosaUnitTag=$unit_tag");
375 }
377 # Setup ldap.conf
378 open($ldap_file, ">","$ldap_config");
379 print $ldap_file "# This file was automatically generated by gosa-si-client. Do not change.\n";
380 print $ldap_file "URI";
382 foreach $element (@ldap_uris) {
383 print $ldap_file " $element";
384 }
386 print $ldap_file "\nBASE $ldap_base\n";
387 foreach $element (@ldap_options) {
388 print $ldap_file "$element\n";
389 }
391 close ($ldap_file);
392 daemon_log("INFO: Wrote $ldap_config", 5);
394 # Setup pam_ldap.conf / libnss-ldap.conf
395 open($pam_file, ">","$pam_config");
396 open($nss_file, ">","$nss_config");
397 print $pam_file "# This file was automatically generated by gosa-si-client. Do not change.\n";
398 print $nss_file "# This file was automatically generated by gosa-si-client. Do not change.\n";
399 print $pam_file "uri";
400 print $nss_file "uri";
402 foreach $element (@ldap_uris) {
403 print $pam_file " $element";
404 print $nss_file " $element";
405 }
407 print $pam_file "\nbase $ldap_base\n";
408 print $nss_file "\nbase $ldap_base\n";
410 foreach $element (@pam_options) {
411 print $pam_file "$element\n";
412 }
414 foreach $element (@nss_options) {
415 print $nss_file "$element\n";
416 }
418 close ($nss_file);
419 daemon_log("INFO: Wrote $nss_config", 5);
420 close ($pam_file);
421 daemon_log("INFO: Wrote $pam_config", 5);
423 # Create goto.secrets if told so - for compatibility reasons
424 if (defined $goto_admin){
425 open($goto_file, ">",$goto_secret_config);
426 print $goto_file "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
427 close($goto_file);
428 chown(0,0, $goto_file);
429 chmod(0600, $goto_file);
430 daemon_log("INFO: Wrote $goto_secret_config", 5);
431 }
433 # Write shell based config
435 # Get first LDAP server
436 my $ldap_server= $ldap_uris[0];
437 $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
439 open($ldap_shell_file, ">","$ldap_shell_config");
440 print $ldap_shell_file "LDAP_BASE=\"$ldap_base\"\n";
441 print $ldap_shell_file "LDAP_SERVER=\"$ldap_server\"\n";
442 print $ldap_shell_file "LDAP_URIS=\"@ldap_uris\"\n";
443 print $ldap_shell_file "ADMIN_BASE=\"$admin_base\"\n";
444 print $ldap_shell_file "DEPARTMENT=\"$department\"\n";
445 print $ldap_shell_file "RELEASE=\"$release\"\n";
446 print $ldap_shell_file "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
447 print $ldap_shell_file "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
448 close($ldap_shell_file);
450 # Set permissions and ownership structure of
451 chown(0, 0, $ldap_shell_file);
452 chmod(0644, $ldap_shell_file);
454 daemon_log("INFO: Wrote $ldap_shell_config", 5);
456 # Write offline config
457 if ($offline_enabled){
459 # Get first LDAP server
460 open( $ldap_offline_file, ">","$ldap_offline_config");
461 print $ldap_offline_file "LDAP_BASE=\"$ldap_base\"\n";
462 print $ldap_offline_file "LDAP_SERVER=\"127.0.0.1\"\n";
463 print $ldap_offline_file "LDAP_URIS=\"ldap://127.0.0.1\"\n";
464 print $ldap_offline_file "ADMIN_BASE=\"$admin_base\"\n";
465 print $ldap_offline_file "DEPARTMENT=\"$department\"\n";
466 print $ldap_offline_file "RELEASE=\"$release\"\n";
467 print $ldap_offline_file "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
468 print $ldap_offline_file "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
469 close($ldap_offline_file);
471 # Set permissions and ownership structure of
472 chown(0, 0, $ldap_offline_file);
473 chmod(0644, $ldap_offline_file);
475 daemon_log("INFO: Wrote $ldap_offline_config", 5);
476 }
480 # Allow custom scripts to be executed
481 if (defined $ldap_config_exit_hook) {
482 system($ldap_config_exit_hook);
483 daemon_log("Hook $ldap_config_exit_hook returned with code ".($? >> 8), 5);
484 }
486 return;
487 }
490 sub new_key {
491 # Create new key
492 my $new_server_key = &main::create_passwd();
494 # Send new_key message to server
495 my $errSend = &main::send_msg_hash_to_target(
496 &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
497 $main::server_address,
498 $main::server_key,
499 );
501 # Set global key
502 if (not $errSend) {
503 $main::server_key = $new_server_key;
504 }
506 return;
507 }
510 sub confirm_new_key {
511 my ($msg, $msg_hash) = @_ ;
512 my $source = @{$msg_hash->{'source'}}[0];
514 &main::daemon_log("confirm new key from $source", 5);
515 return;
517 }
520 sub detect_hardware {
522 &write_to_file('goto-hardware-detection-start', $fai_logpath);
524 my $hwinfo= `which hwinfo`;
525 chomp $hwinfo;
527 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
528 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
529 return;
530 }
532 my $result= {
533 gotoHardwareChecksum => &main::generate_hw_digest(),
534 macAddress => $client_mac_address,
535 gotoXMonitor => "",
536 gotoXDriver => "",
537 gotoXMouseType => "",
538 gotoXMouseport => "",
539 gotoXkbModel => "",
540 gotoXHsync => "",
541 gotoXVsync => "",
542 gotoXResolution => "",
543 ghUsbSupport => "",
544 gotoSndModule => "",
545 ghGfxAdapter => "",
546 ghNetNic => "",
547 ghSoundAdapter => "",
548 ghMemSize => "",
549 ghCpuType => "",
550 gotoModules => [],
551 ghIdeDev => [],
552 ghScsiDev => [],
553 };
555 &main::daemon_log("Starting hardware detection", 4);
556 my $gfxcard= `$hwinfo --gfxcard`;
557 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
558 if(defined($primary_adapter)) {
559 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
560 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
561 }
562 my $monitor= `$hwinfo --monitor`;
563 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
564 if(defined($primary_monitor)) {
565 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
566 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
567 }
569 if(length($result->{gotoXHsync}) == 0) {
570 # set default values
571 $result->{gotoXHsync} = "30+50";
572 $result->{gotoXVsync} = "30+90";
573 }
575 my $mouse= `$hwinfo --mouse`;
576 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
577 if(defined($primary_mouse)) {
578 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
579 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
580 }
582 my $sound= `$hwinfo --sound`;
583 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
584 if(defined($primary_sound)) {
585 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
586 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
587 }
589 my $netcard= `hwinfo --netcard`;
590 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
591 if(defined($primary_netcard)) {
592 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
593 }
595 my $keyboard= `hwinfo --keyboard`;
596 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
597 if(defined($primary_keyboard)) {
598 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
599 }
601 $result->{ghCpuType}= sprintf "%s / %s - %s",
602 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
603 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
605 my @gotoModules=();
606 for my $line(`lsmod`) {
607 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
608 next;
609 } else {
610 push @gotoModules, $1 if $line =~ /^(\w*).*$/
611 }
612 }
613 my %seen = ();
615 # Remove duplicates and save
616 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
618 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
620 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
621 push @{$result->{ghIdeDev}}, $device;
622 }
624 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
625 push @{$result->{ghScsiDev}}, $device;
626 }
628 &main::daemon_log("Hardware detection done!", 4);
630 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
632 &main::send_msg_hash_to_target(
633 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
634 $main::server_address,
635 $main::server_key,
636 );
638 return;
639 }
642 sub ping {
643 my ($msg, $msg_hash) = @_ ;
644 my $header = @{$msg_hash->{'header'}}[0];
645 my $source = @{$msg_hash->{'source'}}[0];
646 my $target = @{$msg_hash->{'target'}}[0];
647 my $session_id = @{$msg_hash->{'session_id'}}[0];
648 my $out_msg;
649 my $out_hash;
651 # there is no session_id so send 'got_new_ping'-msg
652 if (not defined $session_id) {
653 $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
655 # there is a session_id so send 'answer_$session_id'-msg because there is
656 # a process waiting for this message
657 } else {
658 $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
659 &add_content2xml_hash($out_hash, "session_id", $session_id);
660 }
662 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
663 if (defined $forward_to_gosa) {
664 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
665 }
666 $out_msg = &main::create_xml_string($out_hash);
667 return $out_msg;
669 }
671 1;