a6f96e3bc441a3a5e96b837707fa17555cf3f299
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("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 STDERR Dumper(PerlIO::get_layers(file1, output => 1));
241 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
242 print file1 "URI";
243 foreach $element (@ldap_uris) {
244 print file1 " $element";
245 }
246 print file1 "\nBASE $ldap_base\n";
247 foreach $element (@ldap_options) {
248 print file1 "$element\n";
249 }
250 close (file1);
251 daemon_log("wrote $ldap_config", 5);
253 # Setup pam_ldap.conf / libnss-ldap.conf
254 open(file1, "> $pam_config");
255 open(file2, "> $nss_config");
256 print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
257 print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
258 print file1 "uri";
259 print file2 "uri";
260 foreach $element (@ldap_uris) {
261 print file1 " $element";
262 print file2 " $element";
263 }
264 print file1 "\nbase $ldap_base\n";
265 print file2 "\nbase $ldap_base\n";
266 foreach $element (@pam_options) {
267 print file1 "$element\n";
268 }
269 foreach $element (@nss_options) {
270 print file2 "$element\n";
271 }
272 close (file2);
273 daemon_log("wrote $nss_config", 5);
274 close (file1);
275 daemon_log("wrote $pam_config", 5);
277 # Create goto.secrets if told so - for compatibility reasons
278 if (defined $goto_admin){
279 open(file1, "> /etc/goto/secret");
280 close(file1);
281 chown(0,0, "/etc/goto/secret");
282 chmod(0600, "/etc/goto/secret");
283 open(file1, "> /etc/goto/secret");
284 print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
285 close(file1);
286 daemon_log("wrote /etc/goto/secret", 5);
287 }
289 # Write shell based config
290 my $cfg_name= "/etc/ldap/ldap-shell.conf";
292 # Get first LDAP server
293 my $ldap_server= $ldap_uris[0];
294 $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
296 open(file1, "> $cfg_name");
297 print file1 "LDAP_BASE=\"$ldap_base\"\n";
298 print file1 "LDAP_SERVER=\"$ldap_server\"\n";
299 print file1 "LDAP_URIS=\"@ldap_uris\"\n";
300 print file1 "ADMIN_BASE=\"$admin_base\"\n";
301 print file1 "DEPARTMENT=\"$department\"\n";
302 print file1 "RELEASE=\"$release\"\n";
303 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
304 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
305 close(file1);
306 daemon_log("wrote $cfg_name", 5);
308 # Write offline config
309 if ($offline_enabled){
310 $cfg_name= "/etc/ldap/ldap-offline.conf";
312 # Get first LDAP server
313 open(file1, "> $cfg_name");
314 print file1 "LDAP_BASE=\"$ldap_base\"\n";
315 print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
316 print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
317 print file1 "ADMIN_BASE=\"$admin_base\"\n";
318 print file1 "DEPARTMENT=\"$department\"\n";
319 print file1 "RELEASE=\"$release\"\n";
320 print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
321 print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
322 close(file1);
323 daemon_log("wrote $cfg_name", 5);
324 }
326 return;
327 }
330 sub new_key {
331 # my ($msg_hash) = @_ ;
332 my $new_server_key = &main::create_passwd();
334 my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);
335 my $out_msg = &create_xml_string($out_hash);
337 # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg
338 # comes from gosa-si-server within a given time
341 return $out_msg;
342 }
345 sub confirm_new_key {
346 my ($msg, $msg_hash) = @_ ;
347 my $header = @{$msg_hash->{'header'}}[0];
348 my $target = @{$msg_hash->{'target'}}[0];
349 my $source = @{$msg_hash->{'source'}}[0];
351 &main::daemon_log("confirm new key from $source", 5);
352 return;
354 }
357 sub detect_hardware {
359 &write_to_file('goto-hardware-detection-start', $fai_logpath);
361 my $hwinfo= `which hwinfo`;
362 chomp $hwinfo;
364 if (!(defined($hwinfo) && length($hwinfo) > 0)) {
365 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
366 return;
367 }
369 my $result= {
370 gotoHardwareChecksum => &main::generate_hw_digest(),
371 macAddress => $client_mac_address,
372 gotoXMonitor => "",
373 gotoXDriver => "",
374 gotoXMouseType => "",
375 gotoXMouseport => "",
376 gotoXkbModel => "",
377 gotoXHsync => "",
378 gotoXVsync => "",
379 gotoXResolution => "",
380 ghUsbSupport => "",
381 gotoSndModule => "",
382 ghGfxAdapter => "",
383 ghNetNic => "",
384 ghSoundAdapter => "",
385 ghMemSize => "",
386 ghCpuType => "",
387 gotoModules => [],
388 ghIdeDev => [],
389 ghScsiDev => [],
390 };
392 &main::daemon_log("Starting hardware detection", 4);
393 my $gfxcard= `$hwinfo --gfxcard`;
394 my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
395 if(defined($primary_adapter)) {
396 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if
397 $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
398 }
399 my $monitor= `$hwinfo --monitor`;
400 my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
401 if(defined($primary_monitor)) {
402 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if
403 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
404 }
406 if(length($result->{gotoXHsync}) == 0) {
407 # set default values
408 $result->{gotoXHsync} = "30+50";
409 $result->{gotoXVsync} = "30+90";
410 }
412 my $mouse= `$hwinfo --mouse`;
413 my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
414 if(defined($primary_mouse)) {
415 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
416 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
417 }
419 my $sound= `$hwinfo --sound`;
420 my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
421 if(defined($primary_sound)) {
422 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if
423 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
424 }
426 my $netcard= `hwinfo --netcard`;
427 my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
428 if(defined($primary_netcard)) {
429 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
430 }
432 my $keyboard= `hwinfo --keyboard`;
433 my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
434 if(defined($primary_keyboard)) {
435 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
436 }
438 $result->{ghCpuType}= sprintf "%s / %s - %s",
439 `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
440 $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
442 my @gotoModules=();
443 for my $line(`lsmod`) {
444 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
445 next;
446 } else {
447 push @gotoModules, $1 if $line =~ /^(\w*).*$/
448 }
449 }
450 my %seen = ();
452 # Remove duplicates and save
453 push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
455 $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
457 foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
458 push @{$result->{ghIdeDev}}, $device;
459 }
461 foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
462 push @{$result->{ghScsiDev}}, $device;
463 }
465 &main::daemon_log("Hardware detection done!", 4);
467 &write_to_file('goto-hardware-detection-stop', $fai_logpath);
469 return &main::send_msg_hash_to_target(
470 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
471 $main::server_address,
472 $main::server_key,
473 );
474 }
477 sub ping {
478 my ($msg, $msg_hash) = @_ ;
479 my $header = @{$msg_hash->{'header'}}[0];
480 my $source = @{$msg_hash->{'source'}}[0];
481 my $target = @{$msg_hash->{'target'}}[0];
482 my $session_id = @{$msg_hash->{'session_id'}}[0];
485 # switch target and source and send msg back
486 my $out_hash = &main::create_xml_hash("got_ping", $target, $source);
487 &add_content2xml_hash($out_hash, "session_id", $session_id);
488 my $out_msg = &main::create_xml_string($out_hash);
489 return $out_msg;
491 }
493 1;