Pārlūkot izejas kodu

The rule of the only mac in the subnet has been implemented for dynamic records.

root 9 mēneši atpakaļ
vecāks
revīzija
84e38856ce
4 mainītis faili ar 65 papildinājumiem un 18 dzēšanām
  1. 1 0
      scripts/eye-statd.pl
  2. 25 3
      scripts/eyelib/database.pm
  3. 37 0
      scripts/fetch_new_arp.pl
  4. 2 15
      scripts/stat-sync.pl

+ 1 - 0
scripts/eye-statd.pl

@@ -618,6 +618,7 @@ if ($traf_record->{direction}) {
 	    }
         $user_ip = $traf_record->{src_ip};
 	$auth_id = new_auth($hdb,$user_ip);
+        if (!$auth_id) { next; }
 	$l_src_ip = $traf_record->{src_ip};
 	$l_dst_ip = $traf_record->{dst_ip};
 	$user_stats{$user_ip}{auth_id}=$auth_id;

+ 25 - 3
scripts/eyelib/database.pm

@@ -58,6 +58,7 @@ insert_record
 apply_device_lock
 set_lock_discovery
 unset_lock_discovery
+find_mac_in_subnet
 IpToStr
 unbind_ports
 resurrection_auth
@@ -1619,7 +1620,7 @@ sub find_mac_in_subnet {
     if (!$ip or !$mac) { return; }
     my $ip_subnet = get_ip_subnet($db, $ip);
     if (!$ip_subnet) { return; }
-    my @t_auth = get_records_sql($db, "SELECT id,mac,user_id FROM User_auth WHERE ip_int>=" . $ip_subnet->{'ip_int_start'} . " and ip_int<=" . $ip_subnet->{'ip_int_stop'} . " and mac='" . $mac . "' and deleted=0 ORDER BY id");
+    my @t_auth = get_records_sql($db, "SELECT * FROM User_auth WHERE ip_int>=" . $ip_subnet->{'ip_int_start'} . " and ip_int<=" . $ip_subnet->{'ip_int_stop'} . " and mac='" . $mac . "' and deleted=0 ORDER BY id");
     my $auth_count = 0;
     my $result;
     $result->{'count'} = 0;
@@ -1627,8 +1628,7 @@ sub find_mac_in_subnet {
         next if (!$row);
         $auth_count++;
         $result->{'count'} = $auth_count;
-        $result->{$auth_count} = $row->{'id'};
-        push(@{$result->{'users_id'}}, $row->{'user_id'});
+        $result->{items}{$auth_count} = $row;
         }
     return $result;
 }
@@ -1710,11 +1710,27 @@ if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
 if (!$new_user_id) { $new_user_id = new_user($db,$new_user_info); }
 
 my $mac_exists=find_mac_in_subnet($db,$ip,$mac);
+if ($mac_exists) {
+    #deleting the user's entry if the address belongs to a dynamic group
+    foreach my $dup_record_id (keys %{$mac_exists->{items}}) {
+        my $dup_record = $mac_exists->{items}{$dup_record_id};
+        next if (!$dup_record);
+        #remove old dynamic record with some mac
+        if ($dup_record->{dynamic}) {
+            delete_user_auth($db,$dup_record->{id});
+            }
+        }
+    }
+
+#recheck
+$mac_exists=find_mac_in_subnet($db,$ip,$mac);
+
 #disable dhcp for same mac in one ip subnet
 if ($mac_exists and $mac_exists->{'count'}) { $new_record->{dhcp}=0; }
 
 #seek old auth with same ip and mac
 my $auth_exists=get_count_records($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
+
 $new_record->{ip_int}=$ip_aton;
 $new_record->{ip}=$ip;
 $new_record->{mac}=$mac;
@@ -1784,6 +1800,12 @@ my $new_user_info=get_new_user_id($db,$ip,undef,undef);
 my $new_user_id;
 if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
 if ($new_user_info->{ou_id}) { $new_user_id = new_user($db,$new_user_info); }
+
+if (is_dynamic_ou($db,$new_user_info->{ou_id})) {
+    db_log_debug($db,"The ip-address $ip belongs to a dynamic group - ignore it.");
+    return;
+    }
+
 my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
 my $timestamp=GetNowTime();
 my $new_record;

+ 37 - 0
scripts/fetch_new_arp.pl

@@ -34,6 +34,43 @@ setpriority(0,0,19);
 
 if ($config_ref{config_mode}) { log_info("System in configuration mode! Skip discovery."); exit; }
 
+
+db_log_verbose($dbh,'Clearing empty records.');
+
+##### clean empty user account and corresponded devices for dynamic users and hotspot ################
+log_info($dbh,'Clearing empty user account and corresponded devices for dynamic users and hotspot');
+my $u_sql = "SELECT * FROM User_list as U WHERE (U.ou_id=".$default_hotspot_ou_id." OR U.ou_id=".$default_user_ou_id.") AND (SELECT COUNT(*) FROM User_auth WHERE User_auth.deleted=0 AND User_auth.user_id = U.id)=0";
+my @u_ref = get_records_sql($dbh,$u_sql);
+foreach my $row (@u_ref) {
+    db_log_info($dbh,"Remove empty dynamic user with id: $row->{id} login: $row->{login}");
+    delete_user($dbh,$row->{id});
+}
+
+##### clean empty user account and corresponded devices ################
+if ($config_ref{clean_empty_user}) {
+    log_info($dbh,'Clearing empty user account and corresponded devices');
+    my $u_sql = "SELECT * FROM User_list as U WHERE U.permanent=0 AND (SELECT COUNT(*) FROM User_auth WHERE User_auth.deleted=0 AND User_auth.user_id = U.id)=0 AND (SELECT COUNT(*) FROM auth_rules WHERE auth_rules.user_id = U.id)=0;";
+    my @u_ref = get_records_sql($dbh,$u_sql);
+    foreach my $row (@u_ref) {
+            db_log_info($dbh,"Remove empty user with id: $row->{id} login: $row->{login}");
+            delete_user($dbh,$row->{id});
+        }
+    }
+
+#clean temporary user auth records
+my $now = DateTime->now(time_zone=>'local');
+my $clear_time =$dbh->quote($now->strftime('%Y-%m-%d %H:%M:%S'));
+my $users_sql = "SELECT * FROM User_auth WHERE deleted=0 AND dynamic=1 AND `eof`<=".$clear_time;
+my @users_auth = get_records_sql($dbh,$users_sql);
+if (@users_auth and scalar @users_auth) {
+    foreach my $row (@users_auth) {
+            delete_user_auth($dbh,$row->{id});
+            db_log_info($dbh,"Removed dynamic user auth record for auth_id: $row->{'id'} by eof time: $row->{'eof'}",$row->{'id'});
+            my $u_count=get_count_records($dbh,'User_auth','deleted=0 and user_id='.$row->{user_id});
+            if (!$u_count) { delete_user($dbh,$row->{'user_id'}); }
+        }
+    }
+
 my %mac_history;
 
 my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());

+ 2 - 15
scripts/stat-sync.pl

@@ -157,23 +157,10 @@ if (!$pid) {
             my @users_auth = get_records_sql($hdb,$users_sql);
             if (@users_auth and scalar @users_auth) {
                     foreach my $row (@users_auth) {
-                        delete_record($hdb,"User_auth","id='".$row->{id}."'");
+                        delete_user_auth($hdb,$row->{id});
                         db_log_info($hdb,"Removed dynamic user auth record for auth_id: $row->{'id'} by eof time: $row->{'eof'}",$row->{'id'});
                         my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$row->{user_id});
-                        if (!$u_count) {
-                                delete_record($hdb,"User_list","id=".$row->{'user_id'});
-                                db_log_info($hdb,"Removed dynamic user id: $row->{'user_id'} by eof time");
-                                #delete binded device
-                                my $user_device = get_record_sql($hdb,"SELECT * FROM devices WHERE user_id=".$row->{id});
-                                if ($user_device) {
-                                        db_log_info($hdb,"Remove corresponded device id: $user_device->{id} name: $user_device->{device_name}");
-                                        unbind_ports($hdb, $user_device->{id});
-                                        do_sql($hdb, "DELETE FROM connections WHERE device_id=".$user_device->{id});
-                                        do_sql($hdb, "DELETE FROM device_l3_interfaces WHERE device_id=".$user_device->{id});
-                                        do_sql($hdb, "DELETE FROM device_ports WHERE device_id=".$user_device->{id});
-                                        delete_record($hdb, "devices", "id=".$user_device->{id});
-                                        }
-                                }
+                        if (!$u_count) { delete_user($hdb,$row->{'user_id'}); }
                         }
                     }
             }