Răsfoiți Sursa

added the ability to delete an entry in a dynamic group via dhcp release event

root 1 an în urmă
părinte
comite
88fbc41a86
2 a modificat fișierele cu 63 adăugiri și 4 ștergeri
  1. 14 4
      scripts/dhcp-log.pl
  2. 49 0
      scripts/eyelib/database.pm

+ 14 - 4
scripts/dhcp-log.pl

@@ -25,6 +25,13 @@ use POSIX;
 use Net::Netmask;
 use Text::Iconv;
 use File::Tail;
+use Fcntl qw(:flock);
+
+open(SELF,"<",$0) or die "Cannot open $0 - $!";
+flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
+
+setpriority(0,0,19);
+
 
 my $mute_time=300;
 
@@ -346,15 +353,18 @@ if (!$pid) {
             if ($type=~/del/i and $auth_id) {
                 if ($auth_record->{dhcp_time} =~ /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/) {
                     my $d_time = mktime($6,$5,$4,$3,$2-1,$1-1900);
-                    if (time()-$d_time>60 and ($auth_ou_id == $default_user_ou_id or $auth_ou_id==$default_hotspot_ou_id)) {
+                    if (time()-$d_time>60 and (is_dynamic_ou($hdb,$auth_ou_id) or is_default_ou($hdb,$auth_ou_id))) {
                         db_log_info($hdb,"Remove user ip record by dhcp release event for dynamic clients id: $auth_id ip: $dhcp_record->{ip}",$auth_id);
                         my $auth_rec;
                         $auth_rec->{dhcp_action}=$type;
                         $auth_rec->{dhcp_time}=$dhcp_event_time;
                         update_record($hdb,'User_auth',$auth_rec,"id=$auth_id");
-                        delete_user_auth($hdb,$auth_id);
-                        my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$auth_record->{'user_id'});
-                        if (!$u_count) { delete_user($hdb,$auth_record->{'user_id'}); }
+                        #remove user auth record if it belongs to the default pool or it is dynamic
+                        if (is_default_ou($hdb,$auth_ou_id) or (is_dynamic_ou($hdb,$auth_ou_id) and $auth_record->{dynamic})) {
+                                delete_user_auth($hdb,$auth_id);
+                                my $u_count=get_count_records($hdb,'User_auth','deleted=0 and user_id='.$auth_record->{'user_id'});
+                                if (!$u_count) { delete_user($hdb,$auth_record->{'user_id'}); }
+                                }
                         }
                     }
                 }

+ 49 - 0
scripts/eyelib/database.pm

@@ -64,6 +64,10 @@ new_auth
 StrToIp
 get_first_line
 is_ad_computer
+get_dynamic_ou
+is_dynamic_ou
+get_default_ou
+is_default_ou
 update_dns_record
 update_dns_record_by_dhcp
 create_dns_cname
@@ -1929,6 +1933,51 @@ $all_networks->add_string($net->{subnet},$net);
 
 }
 
+#--------------------------------------------------------------------------------------------------------------
+
+sub get_dynamic_ou {
+my $db = shift;
+my @dynamic=();
+my @ou_list = get_records_sql($db,"SELECT id FROM OU WHERE dynamic = 1");
+foreach my $group (@ou_list) {
+    next if (!$group);
+    push(@dynamic,$group->{id});
+    }
+return wantarray ? @dynamic : \@dynamic;
+}
+
+#--------------------------------------------------------------------------------------------------------------
+
+sub get_default_ou {
+my $db = shift;
+my @dynamic=();
+my $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_users = 1");
+if (!$ou) { push(@dynamic,0); } else { push(@dynamic,$ou->{'id'}); }
+$ou = get_record_sql($db,"SELECT id FROM OU WHERE default_hotspot = 1");
+if ($ou) { push(@dynamic,$ou->{id}); }
+return wantarray ? @dynamic : \@dynamic;
+}
+
+#--------------------------------------------------------------------------------------------------------------
+
+sub is_dynamic_ou {
+my $db = shift;
+my $ou_id = shift;
+my @dynamic=get_dynamic_ou($db);
+if (in_array(\@dynamic,$ou_id)) { return 1; }
+return 0;
+}
+
+#--------------------------------------------------------------------------------------------------------------
+
+sub is_default_ou {
+my $db = shift;
+my $ou_id = shift;
+my @dynamic=get_default_ou($db);
+if (in_array(\@dynamic,$ou_id)) { return 1; }
+return 0;
+}
+
 #---------------------------------------------------------------------------------------------------------------
 
 sub get_subnets_ref {