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