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",
10 "detect_hardware",
11 "confirm_new_key",
12 "ping",
13 "import_events",
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, $ldap_config, $pam_config, $nss_config, $fai_logpath);
27 my %cfg_defaults = (
28 "client" => {
29 "ldap" => [\$ldap_enabled, 1],
30 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
31 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
32 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
33 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
34 },
35 );
37 BEGIN {}
39 END {}
41 ### Start ######################################################################
43 &main::read_configfile($main::cfg_file, %cfg_defaults);
46 my $server_address = $main::server_address;
47 my $server_key = $main::server_key;
48 my $client_mac_address = $main::client_mac_address;
50 sub write_to_file {
51 my ($string, $file) = @_;
52 my $error = 0;
54 if( not defined $file || not -f $file ) {
55 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
56 $error++;
57 }
58 if( not defined $string || 0 == length($string)) {
59 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
60 $error++;
61 }
63 if( $error == 0 ) {
65 chomp($string);
67 if( not -f $file ) {
68 open (FILE, "$file");
69 close(FILE);
70 }
71 open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
72 print FILE $string."\n";
73 close(FILE);
74 }
76 return;
77 }
80 sub get_events {
81 return \@events;
82 }
84 sub daemon_log {
85 my ($msg, $level) = @_ ;
86 &main::daemon_log($msg, $level);
87 return;
88 }
90 sub registered {
91 my ($msg, $msg_hash) = @_ ;
93 my $header = @{$msg_hash->{'header'}}[0];
94 if( $header eq "registered" ) {
95 my $source = @{$msg_hash->{'source'}}[0];
96 &main::daemon_log("registration at $source",1);
97 $main::server_address = $source;
98 }
100 # set globaly variable client_address
101 my $target = @{$msg_hash->{'target'}}[0];
102 $main::client_address = $target;
104 # set registration_flag to true
105 my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
106 # Write the MAC address to file
107 if(stat($main::opts_file)) {
108 unlink($main::opts_file);
109 }
111 my $opts_file_FH;
112 my $hostname= $main::client_dnsname;
113 $hostname =~ s/\..*$//;
114 $hostname =~ tr/A-Z/a-z/;
115 open($opts_file_FH, ">$main::opts_file");
116 print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
117 print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
118 print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
119 print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
120 if(defined(@{$msg_hash->{'ldap_available'}}) &&
121 @{$msg_hash->{'ldap_available'}}[0] eq "true") {
122 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
123 }
124 if(defined(@{$msg_hash->{'error'}})) {
125 my $errormsg= @{$msg_hash->{'error'}}[0];
126 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
127 &write_to_file($errormsg, $fai_logpath);
128 }
129 close($opts_file_FH);
131 my $out_msg = &create_xml_string($out_hash);
132 return $out_msg;
133 }
135 sub server_leaving {
136 my ($msg_hash) = @_ ;
137 my $source = @{$msg_hash->{'source'}}[0];
138 my $header = @{$msg_hash->{'header'}}[0];
140 daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
141 $main::server_address = "none";
142 $main::server_key = "none";
144 # reinitialization of default values in config file
145 &main::read_configfile;
147 # registrated at new daemon
148 &main::register_at_server();
150 return;
151 }
154 sub new_ldap_config {
155 my ($msg, $msg_hash) = @_ ;
157 if( $ldap_enabled != 1 ) {
158 return;
159 }
161 my $element;
162 my @ldap_uris;
163 my $ldap_base;
164 my @ldap_options;
165 my @pam_options;
166 my @nss_options;
167 my $goto_admin;
168 my $goto_secret;
169 my $admin_base= "";
170 my $department= "";
171 my $release= "";
172 my $unit_tag;
174 # Transform input into array
175 while ( my ($key, $value) = each(%$msg_hash) ) {
176 if ($key =~ /^(source|target|header)$/) {
177 next;
178 }
180 foreach $element (@$value) {
181 if ($key =~ /^ldap_uri$/) {
182 push (@ldap_uris, $element);
183 next;
184 }
185 if ($key =~ /^ldap_base$/) {
186 $ldap_base= $element;
187 next;
188 }
189 if ($key =~ /^goto_admin$/) {
190 $goto_admin= $element;
191 next;
192 }
193 if ($key =~ /^goto_secret$/) {
194 $goto_secret= $element;
195 next;
196 }
197 if ($key =~ /^ldap_cfg$/) {
198 push (@ldap_options, "$element");
199 next;
200 }
201 if ($key =~ /^pam_cfg$/) {
202 push (@pam_options, "$element");
203 next;
204 }
205 if ($key =~ /^nss_cfg$/) {
206 push (@nss_options, "$element");
207 next;
208 }
209 if ($key =~ /^admin_base$/) {
210 $admin_base= $element;
211 next;
212 }
213 if ($key =~ /^department$/) {
214 $department= $element;
215 next;
216 }
217 if ($key =~ /^unit_tag$/) {
218 $unit_tag= $element;
219 next;
220 }
221 if ($key =~ /^release$/) {
222 $release= $element;
223 next;
224 }
225 }
226 }
228 # Unit tagging enabled?
229 if (defined $unit_tag){
230 push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
231 push (@nss_options, "nss_base_passwd $admin_base?sub?gosaUnitTag=$unit_tag");
232 push (@nss_options, "nss_base_group $admin_base?sub?gosaUnitTag=$unit_tag");
233 }
235 # Setup ldap.conf
236 my $file1;
237 my $file2;
238 open(file1, "> $ldap_config");
239 #print STDERR Dumper(PerlIO::get_layers(file1, output => 1));
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 return;
308 }
311 sub new_key {
312 # my ($msg_hash) = @_ ;
313 my $new_server_key = &main::create_passwd();
315 my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);
316 my $out_msg = &create_xml_string($out_hash);
318 # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg
319 # comes from gosa-si-server within a given time
322 return $out_msg;
323 }
326 sub confirm_new_key {
327 my ($msg, $msg_hash) = @_ ;
328 my $header = @{$msg_hash->{'header'}}[0];
329 my $target = @{$msg_hash->{'target'}}[0];
330 my $source = @{$msg_hash->{'source'}}[0];
332 &main::daemon_log("confirm new key from $source", 5);
333 return;
335 }
338 sub detect_hardware {
340 &write_to_file('goto-hardware-detection-start', $fai_logpath);
342 my $hwinfo= `which hwinfo`;
343 chomp $hwinfo;
345 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
346 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
347 return;
348 }
350 my $result= {
351 gotoHardwareChecksum => &main::generate_hw_digest(),
352 macAddress => $client_mac_address,
353 gotoXMonitor => "",
354 gotoXDriver => "",
355 gotoXMouseType => "",
356 gotoXMouseport => "",
357 gotoXkbModel => "",
358 gotoXHsync => "",
359 gotoXVsync => "",
360 gotoXResolution => "",
361 ghUsbSupport => "",
362 gotoSndModule => "",
363 ghGfxAdapter => "",
364 ghNetNic => "",
365 ghSoundAdapter => "",
366 ghMemSize => "",
367 ghCpuType => "",
368 gotoModules => [],
369 ghIdeDev => [],
370 ghScsiDev => [],
371 };
373 &main::daemon_log("Starting hardware detection", 4);
374 my $gfxcard= `$hwinfo --gfxcard`;
375 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
376 if(defined($primary_adapter)) {
377 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
378 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
379 }
380 my $monitor= `$hwinfo --monitor`;
381 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
382 if(defined($primary_monitor)) {
383 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
384 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
385 }
387 if(length($result->{gotoXHsync}) == 0) {
388 # set default values
389 $result->{gotoXHsync} = "30+50";
390 $result->{gotoXVsync} = "30+90";
391 }
393 my $mouse= `$hwinfo --mouse`;
394 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
395 if(defined($primary_mouse)) {
396 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
397 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
398 }
400 my $sound= `$hwinfo --sound`;
401 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
402 if(defined($primary_sound)) {
403 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
404 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
405 }
407 my $netcard= `hwinfo --netcard`;
408 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
409 if(defined($primary_netcard)) {
410 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
411 }
413 my $keyboard= `hwinfo --keyboard`;
414 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
415 if(defined($primary_keyboard)) {
416 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
417 }
419 $result->{ghCpuType}= sprintf "%s / %s - %s",
420 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
421 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
423 my @gotoModules=();
424 for my $line(`lsmod`) {
425 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
426 next;
427 } else {
428 push @gotoModules, $1 if $line =~ /^(\w*).*$/
429 }
430 }
431 my %seen = ();
433 # Remove duplicates and save
434 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
436 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
438 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
439 push @{$result->{ghIdeDev}}, $device;
440 }
442 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
443 push @{$result->{ghScsiDev}}, $device;
444 }
446 &main::daemon_log("Hardware detection done!", 4);
448 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
450 return &main::send_msg_hash_to_target(
451 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
452 $main::server_address,
453 $main::server_key,
454 );
455 }
458 sub ping {
459 my ($msg, $msg_hash) = @_ ;
460 my $header = @{$msg_hash->{'header'}}[0];
461 my $source = @{$msg_hash->{'source'}}[0];
462 my $target = @{$msg_hash->{'target'}}[0];
463 my $session_id = @{$msg_hash->{'session_id'}}[0];
466 # switch target and source and send msg back
467 my $out_hash = &main::create_xml_hash("got_ping", $target, $source);
468 &add_content2xml_hash($out_hash, "session_id", $session_id);
469 my $out_msg = &main::create_xml_string($out_hash);
470 return $out_msg;
472 }
474 1;