1 package ServerPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 # Each module has to have a function 'process_incoming_msg'. This function works as a interface to gosa-sd and recieves the msg hash from gosa-sd. 'process_incoming_function checks, wether it has a function to process the incoming msg and forward the msg to it.
9 use strict;
10 use warnings;
11 use GOSA::GosaSupportDaemon;
12 use IO::Socket::INET;
13 use XML::Simple;
14 use Net::LDAP;
16 BEGIN{}
17 END {}
20 my ($server_activ, $server_port, $server_passwd, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
21 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
22 my $server;
23 my $no_bus;
25 my %cfg_defaults =
26 ("server" =>
27 {"server_activ" => [\$server_activ, "on"],
28 "server_port" => [\$server_port, "20081"],
29 "server_passwd" => [\$server_passwd, ""],
30 "max_clients" => [\$max_clients, 100],
31 "ldap_uri" => [\$ldap_uri, ""],
32 "ldap_base" => [\$ldap_base, ""],
33 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
34 "ldap_admin_password" => [\$ldap_admin_password, ""],
35 },
36 "bus" =>
37 {"bus_activ" => [\$bus_activ, "on"],
38 "bus_passwd" => [\$bus_passwd, ""],
39 "bus_ip" => [\$bus_ip, ""],
40 "bus_port" => [\$bus_port, "20080"],
41 },
42 );
44 ### START #####################################################################
47 # read configfile and import variables
48 &read_configfile();
50 # detect own ip and mac address
51 my ($server_ip, $server_mac_address) = &get_ip_and_mac();
52 if (not defined $server_ip) {
53 die "EXIT: ip address of $0 could not be detected";
54 }
55 &main::daemon_log("server ip address detected: $server_ip", 1);
56 &main::daemon_log("server mac address detected: $server_mac_address", 1);
58 # complete addresses
59 my $server_address = "$server_ip:$server_port";
60 my $bus_address = "$bus_ip:$bus_port";
62 # create general settings for this module
63 my $xml = new XML::Simple();
65 # open server socket
66 if($server_activ eq "on"){
67 &main::daemon_log(" ", 1);
68 $server = IO::Socket::INET->new(LocalPort => $server_port,
69 Type => SOCK_STREAM,
70 Reuse => 1,
71 Listen => 20,
72 );
73 if(not defined $server){
74 &main::daemon_log("cannot be a tcp server at $server_port : $@");
75 } else {
76 &main::daemon_log("start server: $server_address", 1);
77 }
78 }
80 # register at bus
81 if ($main::no_bus > 0) {
82 $bus_activ = "off"
83 }
84 if($bus_activ eq "on") {
85 &main::daemon_log(" ", 1);
86 ®ister_at_bus();
87 }
89 ### functions #################################################################
91 #sub get_module_tags {
92 #
93 # # lese config file aus dort gibt es eine section Basic
94 # # dort stehen drei packettypen, für die sich das modul anmelden kann, gosa-admin-packages,
95 # # server-packages, client-packages
96 # my %tag_hash = (gosa_admin_packages => "yes",
97 # server_packages => "yes",
98 # client_packages => "yes",
99 # );
100 # return \%tag_hash;
101 #}
104 sub get_module_info {
105 my @info = ($server_address,
106 $server_passwd,
107 $server,
108 $server_activ,
109 "socket",
110 );
111 return \@info;
112 }
115 #=== FUNCTION ================================================================
116 # NAME: read_configfile
117 # PARAMETERS: cfg_file - string -
118 # RETURNS: nothing
119 # DESCRIPTION: read cfg_file and set variables
120 #===============================================================================
121 sub read_configfile {
122 my $cfg;
123 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
124 if( -r $main::cfg_file ) {
125 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
126 } else {
127 print STDERR "Couldn't read config file!";
128 }
129 } else {
130 $cfg = Config::IniFiles->new() ;
131 }
132 foreach my $section (keys %cfg_defaults) {
133 foreach my $param (keys %{$cfg_defaults{ $section }}) {
134 my $pinfo = $cfg_defaults{ $section }{ $param };
135 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
136 }
137 }
138 }
141 #=== FUNCTION ================================================================
142 # NAME: get_ip_and_mac
143 # PARAMETERS: nothing
144 # RETURNS: (ip, mac)
145 # DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
146 # of a inet address is returned as well as the mac address in the line
147 # above the inet address
148 #===============================================================================
149 sub get_ip_and_mac {
150 my $ip = "0.0.0.0.0"; # Defualt-IP
151 my $mac = "00:00:00:00:00:00"; # Default-MAC
152 my @ifconfig = qx(/sbin/ifconfig);
153 foreach(@ifconfig) {
154 if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
155 $mac = "$1:$2:$3:$4:$5:$6";
156 next;
157 }
158 if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
159 $ip = "$1.$2.$3.$4";
160 last;
161 }
162 }
163 return ($ip, $mac);
164 }
167 #=== FUNCTION ================================================================
168 # NAME: open_socket
169 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
170 # [PeerPort] string necessary if port not appended by PeerAddr
171 # RETURNS: socket IO::Socket::INET
172 # DESCRIPTION: open a socket to PeerAddr
173 #===============================================================================
174 sub open_socket {
175 my ($PeerAddr, $PeerPort) = @_ ;
176 if(defined($PeerPort)){
177 $PeerAddr = $PeerAddr.":".$PeerPort;
178 }
179 my $socket;
180 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
181 Porto => "tcp" ,
182 Type => SOCK_STREAM,
183 Timeout => 5,
184 );
185 if(not defined $socket) {
186 return;
187 }
188 &main::daemon_log("open_socket to: $PeerAddr", 7);
189 return $socket;
190 }
192 #=== FUNCTION ================================================================
193 # NAME: register_at_bus
194 # PARAMETERS: nothing
195 # RETURNS: nothing
196 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
197 #===============================================================================
198 sub register_at_bus {
200 # create known_daemons entry
201 &main::create_known_daemon($bus_address);
202 &main::add_content2known_daemons(hostname=>$bus_address, status=>"register_at_bus", passwd=>$bus_passwd);
204 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
205 my $answer = "";
206 $answer = &send_msg_hash2address($msg_hash, $bus_address);
207 if ($answer == 0) {
208 &main::daemon_log("register at bus: $bus_address", 1);
209 } else {
210 &main::daemon_log("unable to send 'register'-msg to bus: $bus_address", 1);
211 }
212 return;
213 }
215 #=== FUNCTION ================================================================
216 # NAME: process_incoming_msg
217 # PARAMETERS: crypted_msg - string - incoming crypted message
218 # RETURNS: nothing
219 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
220 #===============================================================================
221 sub process_incoming_msg {
222 my ($crypted_msg) = @_ ;
223 if(not defined $crypted_msg) {
224 &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
225 }
227 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
228 $crypted_msg = $1;
229 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
231 # collect addresses from possible incoming clients
232 my @valid_keys;
233 my @host_keys = keys %$main::known_daemons;
234 foreach my $host_key (@host_keys) {
235 if($host_key =~ "^$host") {
236 push(@valid_keys, $host_key);
237 }
238 }
239 my @client_keys = keys %$main::known_clients;
240 foreach my $client_key (@client_keys) {
241 if($client_key =~ "^$host"){
242 push(@valid_keys, $client_key);
243 }
244 }
245 push(@valid_keys, $server_address);
247 my $l = @valid_keys;
248 my $msg_hash;
249 my $msg_flag = 0;
250 my $msg = "";
252 # determine the correct passwd for deciphering of the incoming msgs
253 foreach my $host_key (@valid_keys) {
254 eval{
255 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
256 my $key_passwd;
257 if (exists $main::known_daemons->{$host_key}) {
258 $key_passwd = $main::known_daemons->{$host_key}->{passwd};
259 } elsif (exists $main::known_clients->{$host_key}) {
260 $key_passwd = $main::known_clients->{$host_key}->{passwd};
261 } elsif ($host_key eq $server_address) {
262 $key_passwd = $server_passwd;
263 }
264 &main::daemon_log("ServerPackage: key_passwd: $key_passwd", 7);
265 my $key_cipher = &create_ciphering($key_passwd);
266 $msg = &decrypt_msg($crypted_msg, $key_cipher);
267 &main::daemon_log("ServerPackages: decrypted msg: $msg", 7);
268 $msg_hash = $xml->XMLin($msg, ForceArray=>1);
269 #my $tmp = printf Dumper $msg_hash;
270 #&main::daemon_log("DEBUG: ServerPackages: xml hash: $tmp", 7);
271 };
272 if($@) {
273 &main::daemon_log("ServerPackage: key raise error: $@", 7);
274 $msg_flag += 1;
275 } else {
276 last;
277 }
278 }
280 if($msg_flag >= $l) {
281 &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
282 &main::daemon_log("$@", 7);
283 return;
284 }
286 # process incoming msg
287 my $header = @{$msg_hash->{header}}[0];
288 my $source = @{$msg_hash->{source}}[0];
290 &main::daemon_log("recieve '$header' at ServerPackages from $host", 1);
291 # &main::daemon_log("ServerPackages: msg from host:", 5);
292 # &main::daemon_log("\t$host", 5);
293 # &main::daemon_log("ServerPackages: header from msg:", 5);
294 # &main::daemon_log("\t$header", 5);
295 &main::daemon_log("ServerPackages: msg to process:", 5);
296 &main::daemon_log("\t$msg", 5);
298 my @targets = @{$msg_hash->{target}};
299 my $len_targets = @targets;
300 if ($len_targets == 0){
301 &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
303 } elsif ($len_targets == 1){
304 # we have only one target symbol
306 my $target = $targets[0];
307 &main::daemon_log("SeverPackages: msg is for:", 7);
308 &main::daemon_log("\t$target", 7);
310 if ($target eq $server_address) {
311 # msg is for server
312 if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
313 elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
314 elsif ($header eq 'who_has') { &who_has($msg_hash) }
315 elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
316 elsif ($header eq 'update_status') { &update_status($msg_hash) }
317 elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
318 elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
319 else { &main::daemon_log("ERROR: ServerPackages: no function assigned to this msg", 5) }
322 } elsif ($target eq "*") {
323 # msg is for all clients
325 my @target_addresses = keys(%$main::known_clients);
326 foreach my $target_address (@target_addresses) {
327 if ($target_address eq $source) { next; }
328 $msg_hash->{target} = [$target_address];
329 &send_msg_hash2address($msg_hash, $target_address);
330 }
331 } else {
332 # msg is for one host
334 if (exists $main::known_clients->{$target}) {
335 &send_msg_hash2address($msg_hash, $target);
336 } elsif (exists $main::known_daemons->{$target}) {
337 # target is known
338 &send_msg_hash2address($msg_hash, $target);
339 } else {
340 # target is not known
341 &main::daemon_log("ERROR: ServerPackages: target $target is not known neither in known_clients nor in known_daemons", 1);
342 }
343 }
344 }
346 return ;
347 }
350 #=== FUNCTION ================================================================
351 # NAME: got_ping
352 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
353 # RETURNS: nothing
354 # DESCRIPTION: process this incoming message
355 #===============================================================================
356 sub got_ping {
357 my ($msg_hash) = @_;
359 my $source = @{$msg_hash->{source}}[0];
360 my $target = @{$msg_hash->{target}}[0];
361 my $header = @{$msg_hash->{header}}[0];
363 if(exists $main::known_daemons->{$source}) {
364 &main::add_content2known_daemons(hostname=>$source, status=>$header);
365 } else {
366 &main::add_content2known_clients(hostname=>$source, status=>$header);
367 }
369 return;
370 }
373 #=== FUNCTION ================================================================
374 # NAME: new_passwd
375 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
376 # RETURNS: nothing
377 # DESCRIPTION: process this incoming message
378 #===============================================================================
379 sub new_passwd {
380 my ($msg_hash) = @_;
382 my $source = @{$msg_hash->{source}}[0];
383 my $passwd = @{$msg_hash->{new_passwd}}[0];
385 if (exists $main::known_daemons->{$source}) {
386 &main::add_content2known_daemons(hostname=>$source, status=>"new_passwd", passwd=>$passwd);
387 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source);
388 &send_msg_hash2address($hash, $source);
390 } elsif (exists $main::known_clients->{$source}) {
391 &main::add_content2known_clients(hostname=>$source, status=>"new_passwd", passwd=>$passwd);
393 } else {
394 &main::daemon_log("ERROR: $source not known, neither in known_daemons nor in known_clients", 1)
395 }
397 return;
398 }
401 #=== FUNCTION ================================================================
402 # NAME: here_i_am
403 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
404 # RETURNS: nothing
405 # DESCRIPTION: process this incoming message
406 #===============================================================================
407 sub here_i_am {
408 my ($msg_hash) = @_;
410 my $source = @{$msg_hash->{source}}[0];
411 my $mac_address = @{$msg_hash->{mac_address}}[0];
412 my $out_hash;
414 # number of known clients
415 my $nu_clients = keys %$main::known_clients;
417 # check wether client address or mac address is already known
418 if (exists $main::known_clients->{$source}) {
419 &main::daemon_log("WARNING: $source is already known as a client", 1);
420 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
421 $nu_clients --;
422 }
424 # number of actual activ clients
425 my $act_nu_clients = $nu_clients;
427 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
428 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
430 if($max_clients <= $act_nu_clients) {
431 my $out_hash = &create_xml_hash("denied", $server_address, $source);
432 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
433 my $passwd = @{$msg_hash->{new_passwd}}[0];
434 &send_msg_hash2address($out_hash, $source, $passwd);
435 return;
436 }
438 # new client accepted
439 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
441 # create known_daemons entry
442 my $events = @{$msg_hash->{events}}[0];
443 &main::create_known_client($source);
444 &main::add_content2known_clients(hostname=>$source, events=>$events, mac_address=>$mac_address,
445 status=>"registered", passwd=>$new_passwd);
447 # return acknowledgement to client
448 $out_hash = &create_xml_hash("registered", $server_address, $source);
449 &send_msg_hash2address($out_hash, $source);
451 # notify registered client to bus
452 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
453 #&main::send_msg_hash2bus($out_hash);
454 &send_msg_hash2address($out_hash, $bus_address);
456 # give the new client his ldap config
457 &new_ldap_config($source);
459 return;
460 }
463 #=== FUNCTION ================================================================
464 # NAME: who_has
465 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
466 # RETURNS: nothing
467 # DESCRIPTION: process this incoming message
468 #===============================================================================
469 sub who_has {
470 my ($msg_hash) = @_ ;
472 # what is your search pattern
473 my $search_pattern = @{$msg_hash->{who_has}}[0];
474 my $search_element = @{$msg_hash->{$search_pattern}}[0];
475 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
477 # scanning known_clients for search_pattern
478 my @host_addresses = keys %$main::known_clients;
479 my $known_clients_entries = length @host_addresses;
480 my $host_address;
481 foreach my $host (@host_addresses) {
482 my $client_element = $main::known_clients->{$host}->{$search_pattern};
483 if ($search_element eq $client_element) {
484 $host_address = $host;
485 last;
486 }
487 }
489 # search was successful
490 if (defined $host_address) {
491 my $source = @{$msg_hash->{source}}[0];
492 my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
493 &add_content2xml_hash($out_msg, "mac_address", $search_element);
494 &send_msg_hash2address($out_msg, $bus_address);
495 }
496 return;
497 }
500 sub who_has_i_do {
501 my ($msg_hash) = @_ ;
502 my $header = @{$msg_hash->{header}}[0];
503 my $source = @{$msg_hash->{source}}[0];
504 my $search_param = @{$msg_hash->{$header}}[0];
505 my $search_value = @{$msg_hash->{$search_param}}[0];
506 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
507 }
510 #=== FUNCTION ================================================================
511 # NAME: new_ldap_config
512 # PARAMETERS: address - string - ip address and port of a host
513 # RETURNS: nothing
514 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
515 #===============================================================================
516 sub new_ldap_config {
517 my ($address) = @_ ;
519 if (not exists $main::known_clients->{$address}) {
520 &main::daemon_log("ERROR: $address does not exist in known_clients, cannot send him his ldap config", 1);
521 return;
522 }
524 my $mac_address = $main::known_clients->{$address}->{"mac_address"};
525 if (not defined $mac_address) {
526 &main::daemon_log("ERROR: no mac address found for client $address", 1);
527 return;
528 }
530 # Build LDAP connection
531 my $ldap;
532 $ldap= Net::LDAP->new($ldap_uri);
534 # Bind to a directory with dn and password
535 my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
537 # Perform search
538 $mesg = $ldap->search( base => $ldap_base,
539 scope => 'sub',
540 attrs => ['dn', 'gotoLdapServer'],
541 filter => "(&(objectClass=GOhard)(macaddress=$mac_address))");
542 $mesg->code && die $mesg->error;
544 # Sanity check
545 if ($mesg->count != 1) {
546 &main::daemon_log("WARNING: client mac address $mac_address not found/not unique", 1);
547 return;
548 }
550 my $entry= $mesg->entry(0);
551 my $dn= $entry->dn;
552 my @servers= $entry->get_value("gotoLdapServer");
553 my @ldap_uris;
554 my $server;
555 my $base;
557 # Do we need to look at an object class?
558 if ($#servers < 1){
559 $mesg = $ldap->search( base => $ldap_base,
560 scope => 'sub',
561 attrs => ['dn', 'gotoLdapServer'],
562 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
563 $mesg->code && die $mesg->error;
565 # Sanity check
566 if ($mesg->count != 1) {
567 &main::daemon_log("WARNING: no LDAP information found for client mac $mac_address", 1);
568 return;
569 }
571 $entry= $mesg->entry(0);
572 $dn= $entry->dn;
573 @servers= $entry->get_value("gotoLdapServer");
574 }
576 @servers= sort (@servers);
578 foreach $server (@servers){
579 $base= $server;
580 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
581 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
582 push (@ldap_uris, $server);
583 }
585 # Unbind
586 $mesg = $ldap->unbind;
588 # Send information
589 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base );
590 send_msg("new_ldap_config", $server_address, $address, \%data);
592 return;
593 }
596 #=== FUNCTION ================================================================
597 # NAME: execute_actions
598 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
599 # RETURNS: nothing
600 # DESCRIPTION: invokes the script specified in msg_hash which is located under
601 # /etc/gosad/actions
602 #===============================================================================
603 sub execute_actions {
604 my ($msg_hash) = @_ ;
605 my $configdir= '/etc/gosad/actions/';
606 my $result;
608 my $header = @{$msg_hash->{header}}[0];
609 my $source = @{$msg_hash->{source}}[0];
610 my $target = @{$msg_hash->{target}}[0];
612 if((not defined $source)
613 && (not defined $target)
614 && (not defined $header)) {
615 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
616 } else {
617 my $parameters="";
618 my @params = @{$msg_hash->{$header}};
619 my $params = join(", ", @params);
620 &main::daemon_log("execute_actions: got parameters: $params", 5);
622 if (@params) {
623 foreach my $param (@params) {
624 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
625 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
626 $parameters.= " ".$param_value;
627 }
628 }
630 my $cmd= $configdir.$header."$parameters";
631 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
632 $result= "";
633 open(PIPE, "$cmd 2>&1 |");
634 while(<PIPE>) {
635 $result.=$_;
636 }
637 close(PIPE);
638 }
640 # process the event result
643 return;
644 }
647 1;