1 package server_server_com;
3 use strict;
4 use warnings;
6 use Exporter;
7 use Data::Dumper;
8 use GOSA::GosaSupportDaemon;
9 use Time::HiRes qw( usleep);
10 use Socket;
12 @ISA = qw(Exporter);
13 my @events = (
14 'information_sharing',
15 'new_server',
16 'confirm_new_server',
17 'new_foreign_client',
18 'trigger_wake',
19 'foreign_job_updates',
20 'confirm_usr_msg',
21 );
22 @EXPORT = @events;
24 BEGIN {}
26 END {}
28 ### Start ######################################################################
30 sub get_events {
31 return \@events;
32 }
35 sub information_sharing {
36 my ($msg, $msg_hash, $session_id) = @_ ;
37 my $header = @{$msg_hash->{'header'}}[0];
38 my $source = @{$msg_hash->{'source'}}[0];
39 my $target = @{$msg_hash->{'target'}}[0];
41 # Handling of msg tag 'new_user'
42 if (exists $msg_hash->{'new_user'}) {
43 my $new_user_list = $msg_hash->{'new_user'};
45 # Sanity check of new_user_list
46 if (ref($new_user_list) eq 'HASH') {
47 &main::daemon_log("$session_id ERROR: 'new_user'-tag in incoming msg has no content!", 1);
49 } else {
50 my @user_list;
51 # Add each user to login_users_db
52 foreach my $new_user_info (@$new_user_list) {
53 my ($client, $user) = split(/;/, $new_user_info);
54 my %add_hash = ( table=>$main::login_users_tn,
55 primkey=> ['client', 'user'],
56 client=>$client,
57 user=>$user,
58 timestamp=>&get_time,
59 regserver=>$source,
60 );
61 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
62 if ($res != 0)
63 {
64 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str", 1);
65 }
66 else
67 {
68 push(@user_list, "'$user' at '$client'");
69 }
70 }
71 &main::daemon_log("$session_id INFO: server '$source' reports the following logged in user: ".join(", ", @user_list), 5);
72 }
73 }
75 # Handling of msg tag 'user_db'
76 if (exists $msg_hash->{'user_db'}) {
77 my $user_db_list = $msg_hash->{'user_db'};
79 # Sanity check of user_db_list
80 if (ref($user_db_list) eq 'HASH') {
81 &main::daemon_log("$session_id ERROR: 'user_db'-tag in incoming msg has no content!", 1);
83 } else {
84 # Delete all old login information
85 my $sql = "DELETE FROM $main::login_users_tn WHERE regserver='$source'";
86 my $res = $main::login_users_db->exec_statement($sql);
88 # Add each user to login_users_db
89 my @user_list;
90 foreach my $user_db_info (@$user_db_list) {
91 my ($client, $user) = split(/;/, $user_db_info);
92 my %add_hash = ( table=>$main::login_users_tn,
93 primkey=> ['client', 'user'],
94 client=>$client,
95 user=>$user,
96 timestamp=>&get_time,
97 regserver=>$source,
98 );
99 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
100 if ($res != 0) {
101 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str", 1);
102 }
103 else
104 {
105 push(@user_list, "'$user' at '$client'");
106 }
107 }
108 &main::daemon_log("$session_id INFO: server '$source' reports the following logged in user: ".join(", ", @user_list), 5);
109 }
110 }
112 return;
113 }
115 sub foreign_job_updates {
116 my ($msg, $msg_hash, $session_id) = @_ ;
117 my $header = @{$msg_hash->{'header'}}[0];
118 my $source = @{$msg_hash->{'source'}}[0];
119 my $target = @{$msg_hash->{'target'}}[0];
121 my @act_keys = keys %$msg_hash;
122 my @jobs;
123 foreach my $key (@act_keys) {
124 if ($key =~ /answer\d+/ ) { push(@jobs, $key); }
125 }
127 foreach my $foreign_job (@jobs) {
129 # add job to job queue
130 my $func_dic = {table=>$main::job_queue_tn,
131 primkey=>['macaddress', 'headertag'],
132 timestamp=>@{@{$msg_hash->{$foreign_job}}[0]->{'timestamp'}}[0],
133 status=>@{@{$msg_hash->{$foreign_job}}[0]->{'status'}}[0],
134 result=>@{@{$msg_hash->{$foreign_job}}[0]->{'result'}}[0],
135 progress=>@{@{$msg_hash->{$foreign_job}}[0]->{'progress'}}[0],
136 headertag=>@{@{$msg_hash->{$foreign_job}}[0]->{'headertag'}}[0],
137 targettag=>@{@{$msg_hash->{$foreign_job}}[0]->{'targettag'}}[0],
138 xmlmessage=>@{@{$msg_hash->{$foreign_job}}[0]->{'xmlmessage'}}[0],
139 macaddress=>@{@{$msg_hash->{$foreign_job}}[0]->{'macaddress'}}[0],
140 plainname=>@{@{$msg_hash->{$foreign_job}}[0]->{'plainname'}}[0],
141 siserver=>$source,
142 modified=>"0",
143 };
144 my $res = $main::job_db->add_dbentry($func_dic);
145 if (not $res == 0) {
146 &main::daemon_log("$session_id ERROR: ServerPackages: process_job_msg: $res", 1);
147 } else {
148 &main::daemon_log("$session_id INFO: ServerPackages: $header, job '".@{@{$msg_hash->{$foreign_job}}[0]->{'headertag'}}[0].
149 "' successfully added to job queue", 5);
150 }
151 }
153 return;
154 }
157 sub new_server {
158 my ($msg, $msg_hash, $session_id) = @_ ;
159 my $header = @{$msg_hash->{'header'}}[0];
160 my $source = @{$msg_hash->{'source'}}[0];
161 my $target = @{$msg_hash->{'target'}}[0];
162 my $key = @{$msg_hash->{'key'}}[0];
163 my $mac = exists $msg_hash->{'macaddress'} ? @{$msg_hash->{'macaddress'}}[0] : "" ;
164 my @clients = exists $msg_hash->{'client'} ? @{$msg_hash->{'client'}} : qw();
165 my @loaded_modules = exists $msg_hash->{'loaded_modules'} ? @{$msg_hash->{'loaded_modules'}} : qw();
167 # Ignor message if I'm already within a registration process for server $source
168 my $check_statement = "SELECT * FROM $main::known_server_tn WHERE status='new_server' AND hostname='$source'";
169 &main::daemon_log("$session_id DEBUG $check_statement", 7);
170 my $check_res = $main::known_server_db->select_dbentry($check_statement);
171 my $blocking_process = keys(%$check_res);
172 if ($blocking_process)
173 {
174 return;
175 }
177 # Sanity check
178 if (ref $key eq 'HASH') {
179 &main::daemon_log("$session_id ERROR: 'new_server'-message from host '$source' contains no key!", 1);
180 return;
181 }
182 # Add foreign server to known_server_db
183 my $new_update_time = &calc_timestamp(&get_time(), 'plus', $main::foreign_servers_register_delay);
184 my $func_dic = {table=>$main::known_server_tn,
185 primkey=>['hostname'],
186 hostname => $source,
187 macaddress => $mac,
188 status => "new_server",
189 hostkey => $key,
190 loaded_modules => join(',', @loaded_modules),
191 timestamp=>&get_time(),
192 update_time=>$new_update_time,
193 };
194 my $res = $main::known_server_db->add_dbentry($func_dic);
195 if (not $res == 0) {
196 &main::daemon_log("$session_id ERROR: server_server_com.pm: cannot add server to known_server_db: $res", 1);
197 } else {
198 &main::daemon_log("$session_id INFO: server_server_com.pm: server '$source' successfully added to known_server_db", 5);
199 }
201 # delete all entries at foreign_clients_db coresponding to this server
202 my $del_sql = "DELETE FROM $main::foreign_clients_tn WHERE regserver='$source' ";
203 my $del_res = $main::foreign_clients_db->exec_statement($del_sql);
205 # add clients of foreign server to known_foreign_clients_db
206 my @sql_list;
207 foreach my $client (@clients) {
208 my @client_details = split(/,/, $client);
210 # workaround to avoid double entries in foreign_clients_db
211 my $del_sql = "DELETE FROM $main::foreign_clients_tn WHERE hostname='".$client_details[0]."'";
212 push(@sql_list, $del_sql);
214 my $sql = "INSERT INTO $main::foreign_clients_tn VALUES ("
215 ."'".$client_details[0]."'," # hostname
216 ."'".$client_details[1]."'," # macaddress
217 ."'".$source."'," # regserver
218 ."'".&get_time()."')"; # timestamp
219 push(@sql_list, $sql);
220 }
221 if (@sql_list) {
222 my $len = @sql_list;
223 $len /= 2;
224 &main::daemon_log("$session_id DEBUG: Inserting ".$len." entries to foreign_clients_db", 8);
225 my $res = $main::foreign_clients_db->exec_statementlist(\@sql_list);
226 }
228 # fetch all registered clients
229 my $client_sql = "SELECT * FROM $main::known_clients_tn";
230 my $client_res = $main::known_clients_db->exec_statement($client_sql);
233 # add already connected clients to registration message
234 my $myhash = &create_xml_hash('confirm_new_server', $main::server_address, $source);
235 &add_content2xml_hash($myhash, 'key', $key);
236 map(&add_content2xml_hash($myhash, 'client', @{$_}[0].",".@{$_}[4]), @$client_res);
238 # add locally loaded gosa-si modules to registration message
239 my $loaded_modules = {};
240 while (my ($package, $pck_info) = each %$main::known_modules) {
241 foreach my $act_module (keys(%{@$pck_info[2]})) {
242 $loaded_modules->{$act_module} = "";
243 }
244 }
245 map(&add_content2xml_hash($myhash, "loaded_modules", $_), keys(%$loaded_modules));
247 # add macaddress to registration message
248 my ($host_ip, $host_port) = split(/:/, $source);
249 my $local_ip = &get_local_ip_for_remote_ip($host_ip);
250 my $network_interface= &get_interface_for_ip($local_ip);
251 my $host_mac = &get_mac_for_interface($network_interface);
252 &add_content2xml_hash($myhash, 'macaddress', $host_mac);
254 # build registration message and send it
255 my $out_msg = &create_xml_string($myhash);
256 my $error = &main::send_msg_to_target($out_msg, $source, $main::ServerPackages_key, 'confirm_new_server', $session_id);
258 return;
259 }
262 sub confirm_new_server {
263 my ($msg, $msg_hash, $session_id) = @_ ;
264 my $header = @{$msg_hash->{'header'}}[0];
265 my $source = @{$msg_hash->{'source'}}[0];
266 my $key = @{$msg_hash->{'key'}}[0];
267 my $mac = exists $msg_hash->{'macaddress'} ? @{$msg_hash->{'macaddress'}}[0] : "" ;
268 my @clients = exists $msg_hash->{'client'} ? @{$msg_hash->{'client'}} : qw();
269 my @loaded_modules = exists $msg_hash->{'loaded_modules'} ? @{$msg_hash->{'loaded_modules'}} : qw();
271 my $new_update_time = &calc_timestamp(&get_time(), 'plus', $main::foreign_servers_register_delay);
272 my $sql = "UPDATE $main::known_server_tn".
273 " SET status='$header', hostkey='$key', loaded_modules='".join(",",@loaded_modules)."', macaddress='$mac', update_time='$new_update_time'".
274 " WHERE hostname='$source'";
275 my $res = $main::known_server_db->update_dbentry($sql);
277 # add clients of foreign server to known_foreign_clients_db
278 my @sql_list;
279 foreach my $client (@clients) {
280 my @client_details = split(/,/, $client);
282 # workaround to avoid double entries in foreign_clients_db
283 my $del_sql = "DELETE FROM $main::foreign_clients_tn WHERE hostname='".$client_details[0]."'";
284 push(@sql_list, $del_sql);
286 my $sql = "INSERT INTO $main::foreign_clients_tn VALUES ("
287 ."'".$client_details[0]."'," # hostname
288 ."'".$client_details[1]."'," # macaddress
289 ."'".$source."'," # regserver
290 ."'".&get_time()."')"; # timestamp
291 push(@sql_list, $sql);
292 }
293 if (@sql_list) {
294 my $len = @sql_list;
295 $len /= 2;
296 &main::daemon_log("$session_id DEBUG: Inserting ".$len." entries to foreign_clients_db", 8);
297 my $res = $main::foreign_clients_db->exec_statementlist(\@sql_list);
298 }
301 return;
302 }
305 sub new_foreign_client {
306 my ($msg, $msg_hash, $session_id) = @_ ;
307 my $header = @{$msg_hash->{'header'}}[0];
308 my $source = @{$msg_hash->{'source'}}[0];
309 my $hostname = @{$msg_hash->{'client'}}[0];
310 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
311 # if new client is known in known_clients_db
312 my $check_sql = "SELECT * FROM $main::known_clients_tn WHERE (macaddress LIKE '$macaddress')";
313 my $check_res = $main::known_clients_db->select_dbentry($check_sql);
315 if( (keys(%$check_res) == 1) ) {
316 my $host_key = $check_res->{1}->{'hostkey'};
318 # check if new client is still alive
319 my $client_hash = &create_xml_hash("ping", $main::server_address, $hostname);
320 &add_content2xml_hash($client_hash, 'session_id', $session_id);
321 my $client_msg = &create_xml_string($client_hash);
322 my $error = &main::send_msg_to_target($client_msg, $hostname, $host_key, 'ping', $session_id);
323 my $message_id;
324 my $i = 0;
325 while (1) {
326 $i++;
327 my $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
328 my $res = $main::incoming_db->exec_statement($sql);
329 if (ref @$res[0] eq "ARRAY") {
330 $message_id = @{@$res[0]}[0];
331 last;
332 }
334 # do not run into a endless loop
335 if ($i > 50) { last; }
336 usleep(100000);
337 }
339 # client is alive
340 # -> new_foreign_client will be ignored
341 if (defined $message_id) {
342 &main::daemon_log("$session_id ERROR: At new_foreign_clients: host '$hostname' is reported as a new foreign client, ".
343 "but the host is still registered at this server. So, the new_foreign_client-msg will be ignored: $msg", 1);
344 }
345 }
348 # new client is not found in known_clients_db or
349 # new client is dead -> new_client-msg from foreign server is valid
350 # -> client will be deleted from known_clients_db
351 # -> inserted to foreign_clients_db
353 my $del_sql = "DELETE FROM $main::known_clients_tn WHERE (hostname='$hostname')";
354 my $del_res = $main::known_clients_db->exec_statement($del_sql);
355 my $func_dic = { table => $main::foreign_clients_tn,
356 primkey => ['hostname'],
357 hostname => $hostname,
358 macaddress => $macaddress,
359 regserver => $source,
360 timestamp => &get_time(),
361 };
362 my $res = $main::foreign_clients_db->add_dbentry($func_dic);
363 if (not $res == 0) {
364 &main::daemon_log("$session_id ERROR: server_server_com.pm: cannot add server to foreign_clients_db: $res", 1);
365 } else {
366 &main::daemon_log("$session_id INFO: server_server_com.pm: client '$hostname' successfully added to foreign_clients_db", 5);
367 }
369 return;
370 }
373 sub trigger_wake {
374 my ($msg, $msg_hash, $session_id) = @_ ;
376 foreach (@{$msg_hash->{'macaddress'}}){
377 &main::daemon_log("$session_id INFO: trigger wake for $_", 5);
378 my $host = $_;
379 my $ipaddr = '255.255.255.255';
380 my $port = getservbyname('discard', 'udp');
381 if (not defined $port) {
382 &main::daemon_log("$session_id ERROR: cannot determine port for wol $_: 'getservbyname('discard', 'udp')' failed!",1);
383 next;
384 }
386 my ($raddr, $them, $proto);
387 my ($hwaddr, $hwaddr_re, $pkt);
389 # get the hardware address (ethernet address)
390 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
391 if ($host =~ m/^$hwaddr_re$/) {
392 $hwaddr = $host;
393 } else {
394 &main::daemon_log("$session_id ERROR: trigger_wake called with non mac address", 1);
395 }
397 # Generate magic sequence
398 foreach (split /:/, $hwaddr) {
399 $pkt .= chr(hex($_));
400 }
401 $pkt = chr(0xFF) x 6 . $pkt x 16 . $main::wake_on_lan_passwd;
403 # Allocate socket and send packet
405 $raddr = gethostbyname($ipaddr);
406 if (not defined $raddr) {
407 &main::daemon_log("$session_id ERROR: cannot determine raddr for wol $_: 'gethostbyname($ipaddr)' failed!", 1);
408 next;
409 }
411 $them = pack_sockaddr_in($port, $raddr);
412 $proto = getprotobyname('udp');
414 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
415 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
416 send(S, $pkt, 0, $them) or die "send : $!";
417 close S;
418 }
420 return;
421 }
424 sub confirm_usr_msg {
425 my ($msg, $msg_hash, $session_id) = @_ ;
426 &clMessages::confirm_usr_msg($msg, $msg_hash, $session_id);
427 return;
428 }
431 1;