1 package ArpHandler;
3 use strict;
4 use warnings;
6 use Exporter;
7 use GOSA::GosaSupportDaemon;
8 use POSIX;
9 use Fcntl;
10 use Net::LDAP;
11 use Net::LDAP::LDIF;
12 use Net::LDAP::Entry;
13 use Net::DNS;
14 use Switch;
15 use Data::Dumper;
16 use Socket;
18 our @ISA = ("Exporter");
20 # Don't start if some of the modules are missing
21 my $start_service=1;
22 my $lookup_vendor=1;
23 BEGIN{
24 unless(eval('use Socket qw(PF_INET SOCK_DGRAM inet_ntoa sockaddr_in)')) {
25 $start_service=0;
26 }
27 unless(eval('use POE qw(Component::Pcap Component::ArpWatch)')) {
28 $start_service=0;
29 }
30 unless(eval('use Net::MAC::Vendor')) {
31 $lookup_vendor=0;
32 }
33 }
35 END{}
37 my ($timeout, $mailto, $mailfrom, $user, $group);
38 my ($arp_enabled, $arp_interface, $arp_update, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
39 my $hosts_database={};
40 my $ldap;
42 my %cfg_defaults =
43 (
44 "ArpHandler" => {
45 "enabled" => [\$arp_enabled, "true"],
46 "interface" => [\$arp_interface, "all"],
47 "update-entries" => [\$arp_update, "false"],
48 },
49 "server" => {
50 "ldap-uri" => [\$ldap_uri, ""],
51 "ldap-base" => [\$ldap_base, ""],
52 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
53 "ldap-admin-password" => [\$ldap_admin_password, ""],
54 },
55 );
57 #=== FUNCTION ================================================================
58 # NAME: read_configfile
59 # PARAMETERS: cfg_file - string -
60 # RETURNS: nothing
61 # DESCRIPTION: read cfg_file and set variables
62 #===============================================================================
63 sub local_read_configfile {
64 my $cfg;
65 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
66 if( -r $main::cfg_file ) {
67 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
68 } else {
69 print STDERR "Couldn't read config file!";
70 }
71 } else {
72 $cfg = Config::IniFiles->new() ;
73 }
74 foreach my $section (keys %cfg_defaults) {
75 foreach my $param (keys %{$cfg_defaults{ $section }}) {
76 my $pinfo = $cfg_defaults{ $section }{ $param };
77 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
78 }
79 }
80 }
82 sub get_module_info {
83 my @info = (undef, undef);
85 &local_read_configfile();
86 # Don't start if some of the modules are missing
87 if(($arp_enabled eq 'true') && $start_service) {
88 if($lookup_vendor) {
89 eval("Net::MAC::Vendor::load_cache('file:///usr/lib/gosa-si/modules/oui.txt')");
90 if($@) {
91 &main::daemon_log("Loading OUI cache file failed! MAC Vendor lookup disabled", 1);
92 $lookup_vendor=0;
93 } else {
94 &main::daemon_log("Loading OUI cache file suceeded!", 6);
95 }
96 }
98 # When interface is not configured (or 'all'), start arpwatch on all possible interfaces
99 if ((!defined($arp_interface)) || $arp_interface eq 'all') {
100 foreach my $device(&get_interfaces) {
101 # TODO: Need a better workaround for IPv4-to-IPv6 bridges
102 if($device =~ m/^sit\d+$/) {
103 next;
104 }
106 # If device has a valid mac address
107 # TODO: Check if this should be the right way
108 if(not(&get_mac($device) eq "00:00:00:00:00:00")) {
109 &main::daemon_log("Starting ArpWatch on $device", 1);
110 POE::Session->create(
111 inline_states => {
112 _start => sub {
113 &start(@_,$device);
114 },
115 _stop => sub {
116 $_[KERNEL]->post( sprintf("arp_watch_$device") => 'shutdown' )
117 },
118 got_packet => \&got_packet,
119 },
120 );
121 }
122 }
123 } else {
124 foreach my $device(split(/[\s,]+/, $arp_interface)) {
125 &main::daemon_log("Starting ArpWatch on $device", 1);
126 POE::Session->create(
127 inline_states => {
128 _start => sub {
129 &start(@_,$device);
130 },
131 _stop => sub {
132 $_[KERNEL]->post( sprintf("arp_watch_$device") => 'shutdown' )
133 },
134 got_packet => \&got_packet,
135 },
136 );
137 }
138 }
139 } else {
140 &main::daemon_log("ArpHandler disabled. Not starting any capture processes");
141 }
142 return \@info;
143 }
145 sub process_incoming_msg {
146 return 1;
147 }
149 sub start {
150 my $device = (exists($_[ARG0])?$_[ARG0]:'eth0');
151 POE::Component::ArpWatch->spawn(
152 Alias => sprintf("arp_watch_$device"),
153 Device => $device,
154 Dispatch => 'got_packet',
155 Session => $_[SESSION],
156 );
158 $_[KERNEL]->post( sprintf("arp_watch_$device") => 'run' );
159 }
161 sub got_packet {
162 my ($kernel, $heap, $sender, $packet) = @_[KERNEL, HEAP, SENDER, ARG0];
164 if( $packet->{source_haddr} eq "00:00:00:00:00:00" ||
165 $packet->{source_haddr} eq "ff:ff:ff:ff:ff:ff" ||
166 $packet->{source_ipaddr} eq "0.0.0.0") {
167 return;
168 }
170 my $capture_device = sprintf "%s", $kernel->alias_list($sender) =~ /^arp_watch_(.*)$/;
172 my $ldap_handle = &main::get_ldap_handle();
173 if(!exists($hosts_database->{$packet->{source_haddr}})) {
174 my $dnsname= gethostbyaddr(inet_aton($packet->{source_ipaddr}), AF_INET) || $packet->{source_ipaddr};
175 my $ldap_result=&get_host_from_ldap($packet->{source_haddr});
176 if(exists($ldap_result->{dn}) and $arp_update eq "true") {
177 $hosts_database->{$packet->{source_haddr}}=$ldap_result;
178 $hosts_database->{$packet->{source_haddr}}->{dnsname}= $dnsname;
179 if(!exists($ldap_result->{ipHostNumber})) {
180 $hosts_database->{$packet->{source_haddr}}->{ipHostNumber}=$packet->{source_ipaddr};
181 } else {
182 if(!($ldap_result->{ipHostNumber} eq $packet->{source_ipaddr})) {
183 &main::daemon_log(
184 "Current IP Address ".$packet->{source_ipaddr}.
185 " of host ".$hosts_database->{$packet->{source_haddr}}->{dnsname}.
186 " differs from LDAP (".$ldap_result->{ipHostNumber}.")", 4);
187 }
188 }
189 $hosts_database->{$packet->{source_haddr}}->{dnsname}=$dnsname;
190 &main::daemon_log("Host was found in LDAP as ".$ldap_result->{dn}, 8);
191 } else {
192 $hosts_database->{$packet->{source_haddr}}={
193 macAddress => $packet->{source_haddr},
194 ipHostNumber => $packet->{source_ipaddr},
195 dnsname => $dnsname,
196 cn => (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\./),
197 macVendor => (($lookup_vendor) ? &get_vendor_for_mac($packet->{source_haddr}) : "Unknown Vendor"),
198 };
199 &main::daemon_log("A DEBUG: Host was not found in LDAP (".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",522);
200 &main::daemon_log(
201 "A INFO: New Host ".($hosts_database->{$packet->{source_haddr}}->{dnsname}).
202 ": ".$hosts_database->{$packet->{source_haddr}}->{ipHostNumber}.
203 "/".$hosts_database->{$packet->{source_haddr}}->{macAddress},5);
204 &add_ldap_entry(
205 $ldap_handle,
206 $ldap_base,
207 $hosts_database->{$packet->{source_haddr}}->{macAddress},
208 'new-system',
209 $hosts_database->{$packet->{source_haddr}}->{ipHostNumber},
210 'interface',
211 $hosts_database->{$packet->{source_haddr}}->{macVendor});
212 }
213 $hosts_database->{$packet->{source_haddr}}->{device}= $capture_device;
214 } else {
215 if(($arp_update eq "true") and !($hosts_database->{$packet->{source_haddr}}->{ipHostNumber} eq $packet->{source_ipaddr})) {
216 &main::daemon_log(
217 "A INFO: IP Address change of MAC ".$packet->{source_haddr}.
218 ": ".$hosts_database->{$packet->{source_haddr}}->{ipHostNumber}.
219 "->".$packet->{source_ipaddr}, 5);
220 $hosts_database->{$packet->{source_haddr}}->{ipHostNumber}= $packet->{source_ipaddr};
221 &change_ldap_entry(
222 $ldap_handle,
223 $ldap_base,
224 $hosts_database->{$packet->{source_haddr}}->{macAddress},
225 'ip-changed',
226 $hosts_database->{$packet->{source_haddr}}->{ipHostNumber},
227 );
229 }
230 &main::daemon_log("Host already in cache (".($hosts_database->{$packet->{source_haddr}}->{device})."->".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",8);
231 }
232 }
234 sub get_host_from_ldap {
235 my $mac=shift;
236 my $result={};
238 my $ldap_handle = &main::get_ldap_handle();
239 if(defined($ldap_handle)) {
240 my $ldap_result= &search_ldap_entry(
241 $ldap_handle,
242 $ldap_base,
243 "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"
244 );
246 if(defined($ldap_result) && $ldap_result->count==1) {
247 if(exists($ldap_result->{entries}[0]) &&
248 exists($ldap_result->{entries}[0]->{asn}->{objectName}) &&
249 exists($ldap_result->{entries}[0]->{asn}->{attributes})) {
251 for my $attribute(@{$ldap_result->{entries}[0]->{asn}->{attributes}}) {
252 if($attribute->{type} eq 'cn') {
253 $result->{cn} = $attribute->{vals}[0];
254 }
255 if($attribute->{type} eq 'macAddress') {
256 $result->{macAddress} = $attribute->{vals}[0];
257 }
258 if($attribute->{type} eq 'dhcpHWAddress') {
259 $result->{dhcpHWAddress} = $attribute->{vals}[0];
260 }
261 if($attribute->{type} eq 'ipHostNumber') {
262 $result->{ipHostNumber} = $attribute->{vals}[0];
263 }
264 }
265 }
266 $result->{dn} = $ldap_result->{entries}[0]->{asn}->{objectName};
267 }
268 }
270 return $result;
271 }
274 #=== FUNCTION ================================================================
275 # NAME: get_mac
276 # PARAMETERS: interface name (i.e. eth0)
277 # RETURNS: (mac address)
278 # DESCRIPTION: Uses ioctl to get mac address directly from system.
279 #===============================================================================
280 sub get_mac {
281 my $ifreq= shift;
282 my $result;
283 if ($ifreq && length($ifreq) > 0) {
284 if($ifreq eq "all") {
285 $result = "00:00:00:00:00:00";
286 } else {
287 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
289 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
290 or die "socket: $!";
292 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
293 my ($if, $mac)= unpack 'h36 H12', $ifreq;
295 if (length($mac) > 0) {
296 $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
297 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
298 $result = $mac;
299 }
300 }
301 }
302 }
303 return $result;
304 }
306 sub get_vendor_for_mac {
307 my $mac=shift;
308 my $result="Unknown Vendor";
310 if(defined($mac)) {
311 my $vendor= Net::MAC::Vendor::fetch_oui_from_cache(Net::MAC::Vendor::normalize_mac($mac));
312 if(length($vendor) > 0) {
313 $result= @{$vendor}[0];
314 }
315 &main::daemon_log("A INFO: Looking up Vendor for MAC ".$mac.": $result", 5);
316 }
318 return $result;
319 }
321 #=== FUNCTION ================================================================
322 # NAME: add_ldap_entry
323 # PURPOSE: adds an element to ldap-tree
324 # PARAMETERS:
325 # RETURNS: none
326 # DESCRIPTION: ????
327 # THROWS: no exceptions
328 # COMMENTS: none
329 # SEE ALSO: n/a/bin
330 #===============================================================================
331 sub add_ldap_entry {
332 my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip, $interface, $desc) = @_;
333 if(defined($ldap_tree)) {
334 my $dn = "cn=".$hosts_database->{$mac}->{cn}.",ou=incoming,$ldap_base";
335 my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))");
336 my $c_res = (defined($s_res))?$s_res->count:0;
337 if($c_res == 1) {
338 &main::daemon_log("A WARNING: macAddress $mac already in LDAP", 3);
339 return;
340 } elsif($c_res > 0) {
341 &main::daemon_log("A ERROR: macAddress $mac exists $c_res times in LDAP", 1);
342 return;
343 }
345 # create LDAP entry
346 my $entry = Net::LDAP::Entry->new( $dn );
347 $entry->dn($dn);
348 $entry->add("objectClass" => "GOhard");
349 $entry->add("cn" => $hosts_database->{$mac}->{cn});
350 $entry->add("macAddress" => $mac);
351 if(defined $gotoSysStatus) {$entry->add("gotoSysStatus" => $gotoSysStatus)}
352 if(defined $ip) {$entry->add("ipHostNumber" => $ip) }
353 #if(defined $interface) { }
354 if(defined $desc) {$entry->add("description" => $desc) }
356 # submit entry to LDAP
357 my $result = $entry->update ($ldap_tree);
359 # for $result->code constants please look at Net::LDAP::Constant
360 if($result->code == 68) { # entry already exists
361 &main::daemon_log("A WARNING: $dn ".$result->error, 3);
362 } elsif($result->code == 0) { # everything went fine
363 &main::daemon_log("A INFO: Add entry $dn to ldap", 5);
364 } else { # if any other error occur
365 &main::daemon_log("A ERROR: $dn, ".$result->code.", ".$result->error, 1);
366 }
367 } else {
368 &main::daemon_log("A INFO: Not adding new Entry: LDAP disabled", 5);
369 }
370 return;
371 }
374 #=== FUNCTION ================================================================
375 # NAME: change_ldap_entry
376 # PURPOSE: ????
377 # PARAMETERS: ????
378 # RETURNS: ????
379 # DESCRIPTION: ????
380 # THROWS: no exceptions
381 # COMMENTS: none
382 # SEE ALSO: n/a
383 #===============================================================================
384 sub change_ldap_entry {
385 my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip) = @_;
387 if(defined($ldap_tree)) {
388 # check if ldap_entry exists or not
389 my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))");
390 my $c_res = (defined $s_res)?$s_res->count:0;
391 if($c_res == 0) {
392 &main::daemon_log("WARNING: macAddress $mac not in LDAP", 1);
393 return;
394 } elsif($c_res > 1) {
395 &main::daemon_log("ERROR: macAddress $mac exists $c_res times in LDAP", 1);
396 return;
397 }
399 my $s_res_entry = $s_res->pop_entry();
400 my $dn = $s_res_entry->dn();
401 my $replace = {
402 'gotoSysStatus' => $gotoSysStatus,
403 };
404 if (defined($ip)) {
405 $replace->{'ipHostNumber'} = $ip;
406 }
407 my $result = $ldap_tree->modify( $dn, replace => $replace );
409 # for $result->code constants please look at Net::LDAP::Constant
410 if($result->code == 32) { # entry doesnt exists
411 &add_ldap_entry($mac, $gotoSysStatus);
412 } elsif($result->code == 0) { # everything went fine
413 &main::daemon_log("entry $dn changed successful", 1);
414 } else { # if any other error occur
415 &main::daemon_log("ERROR: $dn, ".$result->code.", ".$result->error, 1);
416 }
417 } else {
418 &main::daemon_log("Not changing Entry: LDAP disabled", 6);
419 }
421 return;
422 }
424 #=== FUNCTION ================================================================
425 # NAME: search_ldap_entry
426 # PURPOSE: ????
427 # PARAMETERS: [Net::LDAP] $ldap_tree - object of an ldap-tree
428 # string $sub_tree - dn of the subtree the search is performed
429 # string $search_string - either a string or a Net::LDAP::Filter object
430 # RETURNS: [Net::LDAP::Search] $msg - result object of the performed search
431 # DESCRIPTION: ????
432 # THROWS: no exceptions
433 # COMMENTS: none
434 # SEE ALSO: n/a
435 #===============================================================================
436 sub search_ldap_entry {
437 my ($ldap_tree, $sub_tree, $search_string) = @_;
438 my $msg;
439 if(defined($ldap_tree)) {
440 $msg = $ldap_tree->search( # perform a search
441 base => $sub_tree,
442 filter => $search_string,
443 ) or &main::daemon_log("cannot perform search at ldap: $@", 1);
444 }
445 return $msg;
446 }
448 # vim:ts=4:shiftwidth:expandtab
449 1;