68aa78702ed2efa57a751c38b65874034b0ace14
1 package server_server_com;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 'new_server',
6 'confirm_new_server',
7 'new_foreign_client',
8 );
9 @EXPORT = @events;
11 use strict;
12 use warnings;
13 use Data::Dumper;
14 use GOSA::GosaSupportDaemon;
17 BEGIN {}
19 END {}
21 ### Start ######################################################################
23 sub get_events {
24 return \@events;
25 }
28 sub new_server {
29 my ($msg, $msg_hash, $session_id) = @_ ;
30 my $header = @{$msg_hash->{'header'}}[0];
31 my $source = @{$msg_hash->{'source'}}[0];
32 my $target = @{$msg_hash->{'target'}}[0];
33 my $key = @{$msg_hash->{'key'}}[0];
34 my @clients = exists $msg_hash->{'client'} ? @{$msg_hash->{'client'}} : qw();
36 # sanity check
37 if (ref $key eq 'HASH') {
38 &main::daemon_log("$session_id ERROR: 'new_server'-message from host '$source' contains no key!", 1);
39 return;
40 }
42 # add foreign server to known_server_db
43 my $func_dic = {table=>$main::known_server_tn,
44 primkey=>['hostname'],
45 hostname => $source,
46 status => "new_server",
47 hostkey => $key,
48 timestamp=>&get_time(),
49 };
50 my $res = $main::known_server_db->add_dbentry($func_dic);
51 if (not $res == 0) {
52 &main::daemon_log("$session_id ERROR: server_server_com.pm: cannot add server to known_server_db: $res", 1);
53 } else {
54 &main::daemon_log("$session_id INFO: server_server_com.pm: server '$source' successfully added to known_server_db", 5);
55 }
57 # delete all entries at foreign_clients_db coresponding to this server
58 my $del_sql = "DELETE FROM $main::foreign_clients_tn WHERE regserver='$source' ";
59 my $del_res = $main::foreign_clients_db->exec_statement($del_sql);
61 # add clients of foreign server to known_foreign_clients_db
62 my @sql_list;
63 foreach my $client (@clients) {
64 my @client_details = split(/,/, $client);
66 # workaround to avoid double entries in foreign_clients_db
67 my $del_sql = "DELETE FROM $main::foreign_clients_tn WHERE hostname='".$client_details[0]."'";
68 push(@sql_list, $del_sql);
70 my $sql = "INSERT INTO $main::foreign_clients_tn VALUES ("
71 ."'".$client_details[0]."'," # hostname
72 ."'".$client_details[1]."'," # macaddress
73 ."'".$source."'," # regserver
74 ."'".&get_time()."')"; # timestamp
75 push(@sql_list, $sql);
76 }
77 if (@sql_list) {
78 my $len = @sql_list;
79 $len /= 2;
80 &main::daemon_log("$session_id DEBUG: Inserting ".$len." entries to foreign_clients_db", 8);
81 my $res = $main::foreign_clients_db->exec_statementlist(\@sql_list);
82 }
84 # build confirm_new_server message
85 my %data = ( key=>$key );
86 my $out_msg = &build_msg('confirm_new_server', $main::server_address, $source, \%data);
87 my $error = &main::send_msg_to_target($out_msg, $source, $main::ServerPackages_key, 'confirm_new_server', $session_id);
89 return;
90 }
93 sub confirm_new_server {
94 my ($msg, $msg_hash, $session_id) = @_ ;
95 my $header = @{$msg_hash->{'header'}}[0];
96 my $source = @{$msg_hash->{'source'}}[0];
97 my $key = @{$msg_hash->{'key'}}[0];
99 my $sql = "UPDATE $main::known_server_tn SET status='$header', hostkey='$key' WHERE hostname='$source'";
100 my $res = $main::known_server_db->update_dbentry($sql);
102 return;
103 }
106 sub new_foreign_client {
107 my ($msg, $msg_hash, $session_id) = @_ ;
108 my $header = @{$msg_hash->{'header'}}[0];
109 my $source = @{$msg_hash->{'source'}}[0];
110 my $hostname = @{$msg_hash->{'client'}}[0];
111 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
113 my $func_dic = { table => $main::foreign_clients_tn,
114 primkey => ['hostname'],
115 hostname => $hostname,
116 macaddress => $macaddress,
117 regserver => $source,
118 timestamp => &get_time(),
119 };
120 my $res = $main::foreign_clients_db->add_dbentry($func_dic);
121 if (not $res == 0) {
122 &main::daemon_log("$session_id ERROR: server_server_com.pm: cannot add server to foreign_clients_db: $res", 1);
123 } else {
124 &main::daemon_log("$session_id INFO: server_server_com.pm: client '$hostname' successfully added to foreign_clients_db", 5);
125 }
127 return;
128 }
131 1;