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 "import_events", # no implementations
14 );
15 @EXPORT = @events;
17 use strict;
18 use warnings;
19 use Data::Dumper;
20 use Fcntl;
21 use GOSA::GosaSupportDaemon;
22 use File::Basename;
24 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
27 my %cfg_defaults = (
28 "client" => {
29 "ldap" => [\$ldap_enabled, 1],
30 "offline-ldap" => [\$offline_enabled, 0],
31 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
32 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
33 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
34 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
35 },
36 );
38 BEGIN {}
40 END {}
42 ### Start ######################################################################
44 &main::read_configfile($main::cfg_file, %cfg_defaults);
47 my $server_address = $main::server_address;
48 my $server_key = $main::server_key;
49 my $client_mac_address = $main::client_mac_address;
51 sub write_to_file {
52 my ($string, $file) = @_;
53 my $error = 0;
55 if( not defined $file || not -f $file ) {
56 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
57 $error++;
58 }
59 if( not defined $string || 0 == length($string)) {
60 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
61 $error++;
62 }
64 if( $error == 0 ) {
66 chomp($string);
68 if( not -f $file ) {
69 open (FILE, "$file");
70 close(FILE);
71 }
72 open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
73 print FILE $string."\n";
74 close(FILE);
75 }
77 return;
78 }
81 sub get_events {
82 return \@events;
83 }
85 sub daemon_log {
86 my ($msg, $level) = @_ ;
87 &main::daemon_log($msg, $level);
88 return;
89 }
91 sub registered {
92 my ($msg, $msg_hash) = @_ ;
94 my $header = @{$msg_hash->{'header'}}[0];
95 if( $header eq "registered" ) {
96 my $source = @{$msg_hash->{'source'}}[0];
97 &main::daemon_log("INFO: registration at $source",1);
98 $main::server_address = $source;
99 }
101 # set globaly variable client_address
102 my $target = @{$msg_hash->{'target'}}[0];
103 $main::client_address = $target;
105 # set registration_flag to true
106 my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
107 # Write the MAC address to file
108 if(stat($main::opts_file)) {
109 unlink($main::opts_file);
110 }
112 my $opts_file_FH;
113 my $hostname= $main::client_dnsname;
114 $hostname =~ s/\..*$//;
115 $hostname =~ tr/A-Z/a-z/;
116 open($opts_file_FH, ">$main::opts_file");
117 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
118 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
119 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
120 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
121 if(defined(@{$msg_hash->{'ldap_available'}}) &&
122 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
123 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
124 }
125 if(defined(@{$msg_hash->{'error'}})) {
126 my $errormsg= @{$msg_hash->{'error'}}[0];
127 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
128 &write_to_file($errormsg, $fai_logpath);
129 }
130 close($opts_file_FH);
132 my $out_msg = &create_xml_string($out_hash);
133 return $out_msg;
134 }
136 sub server_leaving {
137 my ($msg_hash) = @_ ;
138 my $source = @{$msg_hash->{'source'}}[0];
139 my $header = @{$msg_hash->{'header'}}[0];
141 daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
142 $main::server_address = "none";
143 $main::server_key = "none";
145 # reinitialization of default values in config file
146 &main::read_configfile;
148 # registrated at new daemon
149 &main::register_at_server();
151 return;
152 }
155 sub new_ldap_config {
156 my ($msg, $msg_hash) = @_ ;
158 if( $ldap_enabled != 1 ) {
159 return;
160 }
162 my $element;
163 my @ldap_uris;
164 my $ldap_base;
165 my @ldap_options;
166 my @pam_options;
167 my @nss_options;
168 my $goto_admin;
169 my $goto_secret;
170 my $admin_base= "";
171 my $department= "";
172 my $release= "";
173 my $unit_tag;
175 # Transform input into array
176 while ( my ($key, $value) = each(%$msg_hash) ) {
177 if ($key =~ /^(source|target|header)$/) {
178 next;
179 }
181 foreach $element (@$value) {
182 if ($key =~ /^ldap_uri$/) {
183 push (@ldap_uris, $element);
184 next;
185 }
186 if ($key =~ /^ldap_base$/) {
187 $ldap_base= $element;
188 next;
189 }
190 if ($key =~ /^goto_admin$/) {
191 $goto_admin= $element;
192 next;
193 }
194 if ($key =~ /^goto_secret$/) {
195 $goto_secret= $element;
196 next;
197 }
198 if ($key =~ /^ldap_cfg$/) {
199 push (@ldap_options, "$element");
200 next;
201 }
202 if ($key =~ /^pam_cfg$/) {
203 push (@pam_options, "$element");
204 next;
205 }
206 if ($key =~ /^nss_cfg$/) {
207 push (@nss_options, "$element");
208 next;
209 }
210 if ($key =~ /^admin_base$/) {
211 $admin_base= $element;
212 next;
213 }
214 if ($key =~ /^department$/) {
215 $department= $element;
216 next;
217 }
218 if ($key =~ /^unit_tag$/) {
219 $unit_tag= $element;
220 next;
221 }
222 if ($key =~ /^release$/) {
223 $release= $element;
224 next;
225 }
226 }
227 }
229 # Unit tagging enabled?
230 if (defined $unit_tag){
231 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
232 push (@nss_options, "nss_base_passwd $admin_base?sub?gosaUnitTag=$unit_tag");
233 push (@nss_options, "nss_base_group $admin_base?sub?gosaUnitTag=$unit_tag");
234 }
236 # Setup ldap.conf
237 my $file1;
238 my $file2;
239 open(file1, "> $ldap_config");
240 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
241 print file1 "URI";
242 foreach $element (@ldap_uris) {
243 print file1 " $element";
244 }
245 print file1 "\nBASE $ldap_base\n";
246 foreach $element (@ldap_options) {
247 print file1 "$element\n";
248 }
249 close (file1);
250 daemon_log("wrote $ldap_config", 5);
252 # Setup pam_ldap.conf / libnss-ldap.conf
253 open(file1, "> $pam_config");
254 open(file2, "> $nss_config");
255 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
256 print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
257 print file1 "uri";
258 print file2 "uri";
259 foreach $element (@ldap_uris) {
260 print file1 " $element";
261 print file2 " $element";
262 }
263 print file1 "\nbase $ldap_base\n";
264 print file2 "\nbase $ldap_base\n";
265 foreach $element (@pam_options) {
266 print file1 "$element\n";
267 }
268 foreach $element (@nss_options) {
269 print file2 "$element\n";
270 }
271 close (file2);
272 daemon_log("wrote $nss_config", 5);
273 close (file1);
274 daemon_log("wrote $pam_config", 5);
276 # Create goto.secrets if told so - for compatibility reasons
277 if (defined $goto_admin){
278 open(file1, "> /etc/goto/secret");
279 close(file1);
280 chown(0,0, "/etc/goto/secret");
281 chmod(0600, "/etc/goto/secret");
282 open(file1, "> /etc/goto/secret");
283 print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
284 close(file1);
285 daemon_log("wrote /etc/goto/secret", 5);
286 }
288 # Write shell based config
289 my $cfg_name= "/etc/ldap/ldap-shell.conf";
291 # Get first LDAP server
292 my $ldap_server= $ldap_uris[0];
293 $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
295 open(file1, "> $cfg_name");
296 print file1 "LDAP_BASE=\"$ldap_base\"\n";
297 print file1 "LDAP_SERVER=\"$ldap_server\"\n";
298 print file1 "LDAP_URIS=\"@ldap_uris\"\n";
299 print file1 "ADMIN_BASE=\"$admin_base\"\n";
300 print file1 "DEPARTMENT=\"$department\"\n";
301 print file1 "RELEASE=\"$release\"\n";
302 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
303 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
304 close(file1);
305 daemon_log("wrote $cfg_name", 5);
307 # Write offline config
308 if ($offline_enabled){
309 $cfg_name= "/etc/ldap/ldap-offline.conf";
311 # Get first LDAP server
312 open(file1, "> $cfg_name");
313 print file1 "LDAP_BASE=\"$ldap_base\"\n";
314 print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
315 print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
316 print file1 "ADMIN_BASE=\"$admin_base\"\n";
317 print file1 "DEPARTMENT=\"$department\"\n";
318 print file1 "RELEASE=\"$release\"\n";
319 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
320 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
321 close(file1);
322 daemon_log("wrote $cfg_name", 5);
323 }
325 return;
326 }
329 sub new_key {
330 # my ($msg_hash) = @_ ;
331 my $new_server_key = &main::create_passwd();
333 my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);
334 my $out_msg = &create_xml_string($out_hash);
336 # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg
337 # comes from gosa-si-server within a given time
340 return $out_msg;
341 }
344 sub confirm_new_key {
345 my ($msg, $msg_hash) = @_ ;
346 my $header = @{$msg_hash->{'header'}}[0];
347 my $target = @{$msg_hash->{'target'}}[0];
348 my $source = @{$msg_hash->{'source'}}[0];
350 &main::daemon_log("confirm new key from $source", 5);
351 return;
353 }
356 sub detect_hardware {
358 &write_to_file('goto-hardware-detection-start', $fai_logpath);
360 my $hwinfo= `which hwinfo`;
361 chomp $hwinfo;
363 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
364 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
365 return;
366 }
368 my $result= {
369 gotoHardwareChecksum => &main::generate_hw_digest(),
370 macAddress => $client_mac_address,
371 gotoXMonitor => "",
372 gotoXDriver => "",
373 gotoXMouseType => "",
374 gotoXMouseport => "",
375 gotoXkbModel => "",
376 gotoXHsync => "",
377 gotoXVsync => "",
378 gotoXResolution => "",
379 ghUsbSupport => "",
380 gotoSndModule => "",
381 ghGfxAdapter => "",
382 ghNetNic => "",
383 ghSoundAdapter => "",
384 ghMemSize => "",
385 ghCpuType => "",
386 gotoModules => [],
387 ghIdeDev => [],
388 ghScsiDev => [],
389 };
391 &main::daemon_log("Starting hardware detection", 4);
392 my $gfxcard= `$hwinfo --gfxcard`;
393 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
394 if(defined($primary_adapter)) {
395 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
396 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
397 }
398 my $monitor= `$hwinfo --monitor`;
399 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
400 if(defined($primary_monitor)) {
401 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
402 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
403 }
405 if(length($result->{gotoXHsync}) == 0) {
406 # set default values
407 $result->{gotoXHsync} = "30+50";
408 $result->{gotoXVsync} = "30+90";
409 }
411 my $mouse= `$hwinfo --mouse`;
412 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
413 if(defined($primary_mouse)) {
414 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
415 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
416 }
418 my $sound= `$hwinfo --sound`;
419 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
420 if(defined($primary_sound)) {
421 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
422 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
423 }
425 my $netcard= `hwinfo --netcard`;
426 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
427 if(defined($primary_netcard)) {
428 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
429 }
431 my $keyboard= `hwinfo --keyboard`;
432 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
433 if(defined($primary_keyboard)) {
434 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
435 }
437 $result->{ghCpuType}= sprintf "%s / %s - %s",
438 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
439 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
441 my @gotoModules=();
442 for my $line(`lsmod`) {
443 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
444 next;
445 } else {
446 push @gotoModules, $1 if $line =~ /^(\w*).*$/
447 }
448 }
449 my %seen = ();
451 # Remove duplicates and save
452 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
454 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
456 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
457 push @{$result->{ghIdeDev}}, $device;
458 }
460 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
461 push @{$result->{ghScsiDev}}, $device;
462 }
464 &main::daemon_log("Hardware detection done!", 4);
466 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
468 return &main::send_msg_hash_to_target(
469 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
470 $main::server_address,
471 $main::server_key,
472 );
473 }
476 sub ping {
477 my ($msg, $msg_hash) = @_ ;
478 my $header = @{$msg_hash->{'header'}}[0];
479 my $source = @{$msg_hash->{'source'}}[0];
480 my $target = @{$msg_hash->{'target'}}[0];
481 my $session_id = @{$msg_hash->{'session_id'}}[0];
482 my $out_msg;
483 my $out_hash;
485 # there is no session_id so send 'got_new_ping'-msg
486 if (not defined $session_id) {
487 $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
489 # there is a session_id so send 'answer_$session_id'-msg because there is
490 # a process waiting for this message
491 } else {
492 $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
493 &add_content2xml_hash($out_hash, "session_id", $session_id);
494 }
496 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
497 if (defined $forward_to_gosa) {
498 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
499 }
500 $out_msg = &main::create_xml_string($out_hash);
501 return $out_msg;
503 }
505 1;