488cc14f10fc26fbddf1c0b46ef0b4d0bbf7655f
1 package siTriggered;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "got_ping",
6 "detected_hardware",
7 "trigger_wake",
8 "reload_ldap_config",
9 );
10 @EXPORT = @events;
12 use strict;
13 use warnings;
14 use GOSA::GosaSupportDaemon;
15 use Socket;
18 BEGIN {}
20 END {}
22 ### Start ######################################################################
24 my $ldap_uri;
25 my $ldap_base;
26 my $ldap_admin_dn;
27 my $ldap_admin_password;
28 my $mesg;
30 my %cfg_defaults = (
31 "server" => {
32 "ldap-uri" => [\$ldap_uri, ""],
33 "ldap-base" => [\$ldap_base, ""],
34 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
35 "ldap-admin-password" => [\$ldap_admin_password, ""],
36 },
37 );
38 &read_configfile($main::cfg_file, %cfg_defaults);
41 sub get_events {
42 return \@events;
43 }
46 sub read_configfile {
47 my ($cfg_file, %cfg_defaults) = @_;
48 my $cfg;
50 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
51 if( -r $cfg_file ) {
52 $cfg = Config::IniFiles->new( -file => $cfg_file );
53 } else {
54 &main::daemon_log("ERROR: siTriggered.pm couldn't read config file!", 1);
55 }
56 } else {
57 $cfg = Config::IniFiles->new() ;
58 }
59 foreach my $section (keys %cfg_defaults) {
60 foreach my $param (keys %{$cfg_defaults{ $section }}) {
61 my $pinfo = $cfg_defaults{ $section }{ $param };
62 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
63 }
64 }
65 }
68 sub reload_ldap_config {
69 my ($msg, $msg_hash, $session_id) = @_;
70 my $header = @{$msg_hash->{header}}[0];
71 my $target = @{$msg_hash->{$header}}[0];
73 my $out_msg = &SIPackages::new_ldap_config($target);
74 my @out_msg_l = ( $out_msg );
75 return @out_msg_l;
76 }
79 sub got_ping {
80 my ($msg, $msg_hash, $session_id) = @_;
82 my $source = @{$msg_hash->{source}}[0];
83 my $target = @{$msg_hash->{target}}[0];
84 my $header = @{$msg_hash->{header}}[0];
85 my $act_time = &get_time;
86 my @out_msg_l;
87 my $out_msg;
89 $session_id = @{$msg_hash->{'session_id'}}[0];
91 # check known_clients_db
92 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
93 my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
94 if( 1 == keys %{$query_res} ) {
95 my $sql_statement= "UPDATE known_clients ".
96 "SET status='$header', timestamp='$act_time' ".
97 "WHERE hostname='$source'";
98 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
99 }
101 # check known_server_db
102 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source'";
103 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
104 if( 1 == keys %{$query_res} ) {
105 my $sql_statement= "UPDATE known_server ".
106 "SET status='$header', timestamp='$act_time' ".
107 "WHERE hostname='$source'";
108 my $res = $main::known_server_db->update_dbentry( $sql_statement );
109 }
111 # create out_msg
112 my $out_hash = &create_xml_hash($header, $source, "GOSA");
113 &add_content2xml_hash($out_hash, "session_id", $session_id);
114 $out_msg = &create_xml_string($out_hash);
115 push(@out_msg_l, $out_msg);
117 return @out_msg_l;
118 }
121 sub detected_hardware {
122 my ($msg, $msg_hash, $session_id) = @_ ;
123 my $address = $msg_hash->{source}[0];
124 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
126 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
127 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
129 # check hit
130 my $hit_counter = keys %{$res};
131 if( not $hit_counter == 1 ) {
132 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
133 return;
134 }
136 my $macaddress = $res->{1}->{macaddress};
137 my $hostkey = $res->{1}->{hostkey};
139 if (not defined $macaddress) {
140 &main::daemon_log("ERROR: no mac address found for client $address", 1);
141 return;
142 }
143 # Build LDAP connection
144 &main::refresh_ldap_handle();
145 if( not defined $main::ldap_handle ) {
146 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
147 return;
148 }
150 # Perform search
151 $mesg = $main::ldap_handle->search(
152 base => $ldap_base,
153 scope => 'sub',
154 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
155 );
157 # We need to create a base entry first (if not done from ArpHandler)
158 if($mesg->count == 0) {
159 &main::daemon_log("INFO: Need to create a new LDAP Entry for client $address", 6);
160 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
161 my $dnsname= gethostbyaddr(inet_aton($ipaddress), AF_INET) || $ipaddress;
162 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
163 my $dn = "cn=$cn,ou=incoming,$ldap_base";
164 &main::daemon_log("INFO: Creating entry for $dn",5);
165 my $entry= Net::LDAP::Entry->new( $dn );
166 $entry->dn($dn);
167 $entry->add("objectClass" => "goHard");
168 $entry->add("cn" => $cn);
169 $entry->add("macAddress" => $macaddress);
170 $entry->add("gotomode" => "locked");
171 $entry->add("gotoSysStatus" => "new-system");
172 $entry->add("ipHostNumber" => $ipaddress);
173 if(defined($main::gosa_unit_tag) && length($main::gosa_unit_tag) > 0) {
174 $entry->add("objectClass" => "gosaAdministrativeUnitTag");
175 $entry->add("gosaUnitTag" => $main::gosa_unit_tag);
176 }
177 my $res=$entry->update($main::ldap_handle);
178 if(defined($res->{'errorMessage'}) &&
179 length($res->{'errorMessage'}) >0) {
180 &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
181 return;
182 } else {
183 # Fill $mesg again
184 $mesg = $main::ldap_handle->search(
185 base => $ldap_base,
186 scope => 'sub',
187 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
188 );
189 }
190 }
192 if($mesg->count == 1) {
193 my $entry= $mesg->entry(0);
194 $entry->changetype("modify");
195 foreach my $attribute (
196 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
197 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
198 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
199 if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
200 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
201 if(defined($entry->get_value($attribute))) {
202 $entry->delete($attribute);
203 }
204 &main::daemon_log("INFO: Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},5);
205 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
206 }
207 }
208 foreach my $attribute (
209 "gotoModules", "ghScsiDev", "ghIdeDev") {
210 if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
211 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
212 if(defined($entry->get_value($attribute))) {
213 $entry->delete($attribute);
214 }
215 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
216 $entry->add($attribute => $array_entry);
217 }
218 }
219 }
221 my $res=$entry->update($main::ldap_handle);
222 if(defined($res->{'errorMessage'}) &&
223 length($res->{'errorMessage'}) >0) {
224 &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
225 } else {
226 &main::daemon_log("INFO: Added Hardware configuration to LDAP", 5);
227 }
229 }
230 return ;
231 }
234 sub trigger_wake {
235 my ($msg, $msg_hash, $session_id) = @_ ;
237 foreach (@{$msg_hash->{macAddress}}){
238 &main::daemon_log("INFO: trigger wake for $_", 5);
239 my $host = shift;
240 my $ipaddr = shift || '255.255.255.255';
241 my $port = getservbyname('discard', 'udp');
243 my ($raddr, $them, $proto);
244 my ($hwaddr, $hwaddr_re, $pkt);
246 # get the hardware address (ethernet address)
247 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
248 if ($host =~ m/^$hwaddr_re$/) {
249 $hwaddr = $host;
250 } else {
251 # $host is not a hardware address, try to resolve it
252 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
253 my $ip_addr;
254 if ($host =~ m/^$ip_re$/) {
255 $ip_addr = $host;
256 } else {
257 my $h;
258 unless ($h = gethost($host)) {
259 return undef;
260 }
261 $ip_addr = inet_ntoa($h->addr);
262 }
263 }
265 # Generate magic sequence
266 foreach (split /:/, $hwaddr) {
267 $pkt .= chr(hex($_));
268 }
269 $pkt = chr(0xFF) x 6 . $pkt x 16;
271 # Allocate socket and send packet
273 $raddr = gethostbyname($ipaddr)->addr;
274 $them = pack_sockaddr_in($port, $raddr);
275 $proto = getprotobyname('udp');
277 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
278 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
280 send(S, $pkt, 0, $them) or die "send : $!";
281 close S;
282 }
284 return;
285 }
287 1;