Explorar el Código

- bugfix: decline change user auth record for new ip or mac in static subnet

Dmitriev Roman hace 3 años
padre
commit
c06342e87d

+ 0 - 0
docs/addons/cfg/config → docs/addons/cfg/config.perl


+ 20 - 0
docs/addons/cfg/config.php

@@ -0,0 +1,20 @@
+<?php
+
+if (!defined("CONFIG"))die("Not defined");
+
+$language="russian";
+setlocale(LC_ALL, 'ru_RU.UTF8');
+
+$style="white"; #css ctyle
+
+$dbhost="localhost";
+$dbname="stat";
+$dbuser="rstat";
+$dbpass="password";
+
+$cacti_dbhost="localhost";
+$cacti_dbname="cacti";
+
+$ipcam_group_id=5;
+
+?>

+ 1 - 0
html/admin/users/add_dhcp.php

@@ -38,6 +38,7 @@ if (!empty($_GET["ip"]) and !empty($_GET["mac"])) {
 
 	if ($action ==='add' and empty($auth)) {
 	    $aid = resurrection_auth($db_link, $ip, $mac, $action, $dhcp_hostname);
+	    if (empty($aid)) { return; }
 	    LOG_VERBOSE($db_link,"Add user by dhcp request ip: $ip mac: $mac",$aid);
             }
 

+ 11 - 13
html/inc/common.php

@@ -1306,17 +1306,9 @@ function get_ip_subnet($db,$ip)
 {
 if (empty($ip)) { return; }
 $ip_aton = ip2long($ip);
-$t_option = mysqli_query($db, "SELECT id,subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1 or office=1");
-while (list ($f_net_id,$f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
-    if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
-	    $result['subnet_id']=$f_net_id;
-	    $result['subnet']=$f_net;
-	    $result['int_start']=$f_start;
-	    $result['int_stop']=$f_stop;
-            return $result;
-        }
-    }
-return;
+$user_subnet = get_record_sql($db, "SELECT * FROM `subnets` WHERE hotspot=1 or office=1 and ( $ip_aton >= ip_int_start and $ip_aton <= ip_int_stop)");
+if (empty($user_subnet)) { return; }
+return $user_subnet;
 }
 
 function find_mac_in_subnet($db,$ip,$mac)
@@ -1324,8 +1316,8 @@ function find_mac_in_subnet($db,$ip,$mac)
 if (empty($ip)) { return; }
 if (empty($mac)) { return; }
 $ip_subnet=get_ip_subnet($db,$ip);
-if (!isset($ip_subnet)) { return; }
-$t_auth=get_records_sql($db, "SELECT id,mac,user_id FROM User_auth WHERE ip_int>=".$ip_subnet['int_start']." and ip_int<=".$ip_subnet['int_stop']." and mac='" . $mac . "' and deleted=0 ORDER BY id");
+if (empty($ip_subnet)) { return; }
+$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");
 $auth_count=0;
 $result['count']=0;
 $result['users_id']=[];
@@ -1464,6 +1456,12 @@ function resurrection_auth($db, $ip, $mac, $action, $dhcp_hostname)
         return $auth_record['id'];
 	}
 
+    $ip_subnet=get_ip_subnet($db,$ip);
+    if ($ip_subnet['static']) {
+        LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [".mac_dotted($mac)."]. Skip");
+        return;
+	}
+
     $msg = '';
     // search changed mac
     $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");

+ 11 - 5
scripts/Rstat/mysql.pm

@@ -830,6 +830,12 @@ if ($record->{user_id}) {
     return $record->{id};
     }
 
+my $user_subnet=$office_networks->match_string($ip);
+if ($user_subnet->{static}) {
+    db_log_warning($db,"Unknown ip+mac found in static subnet! Stop work for ip: $ip mac: [".$mac."]");
+    return 0;
+    }
+
 #search changed mac
 $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `ip_int`='.$ip_aton." and deleted=0");
 if ($record->{id}) {
@@ -1097,26 +1103,26 @@ next if (!$net->{subnet});
 $subnets_ref{$net->{subnet}}=$net;
 if ($net->{office}) {
 	push(@office_network_list,$net->{subnet});
-        $office_networks->add_string($net->{subnet});
+        $office_networks->add_string($net->{subnet},$net);
         }
 
 if ($net->{free}) {
 	push(@free_network_list,$net->{subnet});
-        $free_networks->add_string($net->{subnet});
+        $free_networks->add_string($net->{subnet},$net);
         }
 
 if ($net->{vpn}) {
 	push(@vpn_network_list,$net->{subnet});
-        $vpn_networks->add_string($net->{subnet});
+        $vpn_networks->add_string($net->{subnet},$net);
         }
 
 if ($net->{hotspot}) {
         push(@hotspot_network_list,$net->{subnet});
         push(@all_network_list,$net->{subnet});
-        $hotspot_networks->add_string($net->{subnet});
+        $hotspot_networks->add_string($net->{subnet},$net);
         }
 push(@all_network_list,$net->{subnet});
-$all_networks->add_string($net->{subnet});
+$all_networks->add_string($net->{subnet},$net);
 }
 
 }

+ 1 - 0
scripts/dhcp-log.pl

@@ -163,6 +163,7 @@ if (!$pid) {
 
             if ($type eq 'add') {
                 my $res_id = resurrection_auth($hdb,$dhcp_record->{ip},$mac,$type,$dhcp_record->{hostname_utf8});
+                next if (!$res_id);
                 $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id='.$res_id);
                 db_log_info($hdb,"Check for new auth. Found id: $res_id",$res_id);
                 } else { $auth_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE ip="'.$dhcp_record->{ip}.'" and mac="'.$mac.'" and deleted=0 ORDER BY last_found DESC'); }

+ 1 - 0
scripts/fetch_new_arp.pl

@@ -129,6 +129,7 @@ foreach my $arp_table (@arp_array) {
         db_log_debug($dbh,"Analyze ip: $ip mac: $mac") if ($debug);
         my $auth_id = $users->match_string($ip);
         my $cur_auth_id=resurrection_auth($dbh,$ip,$mac,'arp');
+        next if (!$cur_auth_id);
         $mac_history{$simple_mac}{auth_id}=$cur_auth_id;
         if ($auth_id ne $cur_auth_id) { $mac_history{$simple_mac}{changed}=1; }
     }

+ 7 - 2
scripts/update-dnsmasq

@@ -2,10 +2,13 @@
 
 logger -t dhcpd "Refresh dnsmasq config request"
 
+force=$1
+
 /usr/local/scripts/print-dnsmasq.pl >/tmp/mac-all
 ret=$?
 
 if [ ${ret} -ne 0 ]; then
+    logger -t dhcpd "Config generate error! Bye."
     exit
     fi
 
@@ -13,14 +16,16 @@ touch /etc/dnsmasq.d/mac-all
 diff -aqbBfi /tmp/mac-all /etc/dnsmasq.d/mac-all >/dev/null
 ret=$?
 
-if [ ${ret} -ne 0 ]; then
+if [ ${ret} -ne 0 -o -n "${force}" ]; then
     logger -t dhcpd "Update dnsmasq config"
     cat /etc/dnsmasq.d/mac-all >/tmp/mac.old
     cat  /tmp/mac-all >/etc/dnsmasq.d/mac-all
     /usr/sbin/dnsmasq --test >/dev/null 2>&1
     ret=$?
     if [ ${ret} -eq 0 ]; then
-        systemctl restart dnsmasq>/dev/null
+        systemctl stop dnsmasq>/dev/null
+        >/var/lib/misc/dnsmasq.leases
+        systemctl start dnsmasq>/dev/null
 	else
         logger -t dhcpd "Config error! Rollback changes."
 	cat /tmp/mac.old >/etc/dnsmasq.d/mac-all