sync_mikrotik.pl 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use English;
  11. use base;
  12. use FindBin '$Bin';
  13. use lib "/opt/Eye/scripts";
  14. use strict;
  15. use Time::Local;
  16. use FileHandle;
  17. use Data::Dumper;
  18. use eyelib::config;
  19. use eyelib::main;
  20. use eyelib::cmd;
  21. use Net::Patricia;
  22. use Date::Parse;
  23. use eyelib::net_utils;
  24. use eyelib::database;
  25. use eyelib::common;
  26. use DBI;
  27. use Fcntl qw(:flock);
  28. use Parallel::ForkManager;
  29. use Net::DNS;
  30. use Getopt::Long;
  31. #$debug = 1;
  32. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  33. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  34. $|=1;
  35. if (IsNotRun($SPID)) { Add_PID($SPID); } else { die "Warning!!! $SPID already runnning!\n"; }
  36. my $fork_count = $cpu_count*10;
  37. #flag for operation status
  38. my $all_ok = 1;
  39. my $changes_only = 0;
  40. my $router_id = undef;
  41. # Парсим аргументы
  42. GetOptions(
  43. 'changes-only|c' => \$changes_only,
  44. 'router-id|r=i' => \$router_id,
  45. ) or die "Ошибка в параметрах!\n";
  46. my @gateways = ();
  47. #save changed records
  48. my @changes_found = get_records_sql($dbh,"SELECT id, ip FROM user_auth WHERE changed=1");
  49. #@office_network_list - все рабочие сети
  50. if ($changes_only) {
  51. my @all_gateways = get_records_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) AND protocol>=0 AND (user_acl=1 OR dhcp=1) AND deleted=0 AND vendor_id=9' );
  52. my %network_to_routers;
  53. for my $gate (@all_gateways) {
  54. my $router_id = $gate->{id};
  55. my $connected_only = $gate->{connected_user_only} // 0;
  56. my @subnets_for_router=();
  57. if ($connected_only) {
  58. # Только привязанные подсети
  59. my @gw_subnets = get_records_sql($dbh,"SELECT s.subnet FROM gateway_subnets gs JOIN subnets s ON gs.subnet_id = s.id WHERE gs.device_id = ? AND s.subnet IS NOT NULL", $router_id );
  60. @subnets_for_router = map { $_->{subnet} } @gw_subnets;
  61. } else {
  62. # Все офисные сети
  63. push(@subnets_for_router,@office_network_list);
  64. }
  65. # Добавляем роутер ко всем его подсетям
  66. for my $subnet (@subnets_for_router) {
  67. next unless $subnet && $subnet =~ m{^\d+\.\d+\.\d+\.\d+/\d+$};
  68. $network_to_routers{$subnet} //= {};
  69. $network_to_routers{$subnet}{$router_id} = 1;
  70. }
  71. }
  72. my $GwPat = Net::Patricia->new(AF_INET);
  73. for my $subnet (keys %network_to_routers) {
  74. # Храним ссылку на хеш роутеров
  75. $GwPat->add_string($subnet, \%{$network_to_routers{$subnet}});
  76. }
  77. my %selected_router_ids;
  78. for my $user (@changes_found) {
  79. my $ip = $user->{ip};
  80. next unless $ip && $ip =~ /^\d+\.\d+\.\d+\.\d+$/;
  81. my $data_ref = $GwPat->match_string($ip);
  82. if ($data_ref) {
  83. for my $rid (keys %$data_ref) {
  84. if (defined $router_id) {
  85. $selected_router_ids{$rid} = 1 if ($router_id == $rid);
  86. } else {
  87. $selected_router_ids{$rid} = 1;
  88. }
  89. }
  90. }
  91. }
  92. if (%selected_router_ids) {
  93. my @ids = keys %selected_router_ids;
  94. my $ph = join ',', ('?') x @ids;
  95. @gateways = get_records_sql($dbh, "SELECT * FROM devices WHERE id IN ($ph)", @ids );
  96. } else {
  97. @gateways = (); # Нет затронутых роутеров
  98. }
  99. if (!scalar @gateways) { exit 0; }
  100. }
  101. else {
  102. # Если задан router_id — выбираем один роутер
  103. if (defined $router_id) {
  104. my $router = get_record_sql($dbh, 'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) AND protocol>=0 AND (user_acl=1 OR dhcp=1) AND deleted=0 AND vendor_id=9 AND id=?', $router_id );
  105. if ($router) { push(@gateways, $router); }
  106. } else {
  107. # Иначе выбираем все подходящие роутеры
  108. @gateways = get_records_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) AND protocol>=0 AND (user_acl=1 OR dhcp=1) AND deleted=0 AND vendor_id=9' );
  109. }
  110. }
  111. #все сети организации, работающие по dhcp
  112. my $dhcp_networks = new Net::Patricia;
  113. my %dhcp_conf;
  114. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 ORDER BY ip_int_start');
  115. foreach my $subnet (@subnets) {
  116. next if (!$subnet->{gateway});
  117. my $dhcp_info=GetDhcpRange($subnet->{subnet});
  118. $dhcp_networks->add_string($subnet->{subnet},$subnet->{subnet});
  119. $dhcp_conf{$subnet->{subnet}}->{first_pool_ip}=IpToStr($subnet->{dhcp_start});
  120. $dhcp_conf{$subnet->{subnet}}->{last_pool_ip}=IpToStr($subnet->{dhcp_stop});
  121. $dhcp_conf{$subnet->{subnet}}->{relay_ip}=IpToStr($subnet->{gateway});
  122. $dhcp_conf{$subnet->{subnet}}->{gateway}=IpToStr($subnet->{gateway});
  123. #раскрываем подсеть
  124. $dhcp_conf{$subnet->{subnet}}->{network} = $dhcp_info->{network};
  125. $dhcp_conf{$subnet->{subnet}}->{masklen} = $dhcp_info->{masklen};
  126. $dhcp_conf{$subnet->{subnet}}->{first_ip} = $dhcp_info->{first_ip};
  127. $dhcp_conf{$subnet->{subnet}}->{last_ip} = $dhcp_info->{last_ip};
  128. $dhcp_conf{$subnet->{subnet}}->{first_ip_aton}=StrToIp($dhcp_info->{first_ip});
  129. $dhcp_conf{$subnet->{subnet}}->{last_ip_aton}=StrToIp($dhcp_info->{last_ip});
  130. }
  131. my $pm = Parallel::ForkManager->new($fork_count);
  132. foreach my $gate (@gateways) {
  133. next if (!$gate);
  134. my $gate_ident = $gate->{device_name}." [$gate->{ip}]:: ";
  135. $pm->start and next;
  136. $dbh = init_db();
  137. my @cmd_list=();
  138. $gate = netdev_set_auth($gate);
  139. $gate->{login}.='+ct400w';
  140. my $t = netdev_login($gate);
  141. if (!$t) {
  142. log_error($gate_ident."Login to $gate->{device_name} [$gate->{ip}] failed! Skip gateway.");
  143. $dbh->disconnect();
  144. $pm->finish;
  145. next;
  146. }
  147. my $router_name=$gate->{device_name};
  148. my $router_ip=$gate->{ip};
  149. my $shaper_enabled = $gate->{queue_enabled};
  150. my $connected_users_only = $gate->{connected_user_only};
  151. my @changed_ref=();
  152. #все сети роутера, которые к нему подключены по информации из БД - Patricia Object
  153. my $connected_users = new Net::Patricia;
  154. #сети, которые должен отдавать роутер по dhcp - simple hash
  155. my %connected_nets_hash;
  156. #исключения из авторизации хот-спота
  157. my %hotspot_exceptions;
  158. my @lan_int=();
  159. my @wan_int=();
  160. # настройки испольуземых l3-интерфейсов
  161. my %l3_interfaces;
  162. my @l3_int = get_records_sql($dbh,'SELECT * FROM device_l3_interfaces WHERE device_id=?',$gate->{'id'});
  163. foreach my $l3 (@l3_int) {
  164. $l3->{'name'}=~s/\"//g;
  165. $l3_interfaces{$l3->{'name'}}{type} = $l3->{'interface_type'};
  166. $l3_interfaces{$l3->{'name'}}{bandwidth} = 0;
  167. if ($l3->{'bandwidth'}) { $l3_interfaces{$l3->{'name'}}{bandwidth} = $l3->{'bandwidth'}; }
  168. if ($l3->{'interface_type'} eq '0') { push(@lan_int,$l3->{'name'}); }
  169. if ($l3->{'interface_type'} eq '1') { push(@wan_int,$l3->{'name'}); }
  170. }
  171. #формируем список подключенных к роутеру сетей
  172. my @gw_subnets = get_records_sql($dbh,"SELECT gateway_subnets.*,subnets.subnet FROM gateway_subnets LEFT JOIN subnets ON gateway_subnets.subnet_id = subnets.id WHERE gateway_subnets.device_id=?",$gate->{'id'});
  173. if (@gw_subnets and scalar @gw_subnets) {
  174. foreach my $gw_subnet (@gw_subnets) {
  175. if ($gw_subnet and $gw_subnet->{'subnet'}) {
  176. $connected_users->add_string($gw_subnet->{'subnet'});
  177. $connected_nets_hash{$gw_subnet->{'subnet'}} = $gw_subnet;
  178. }
  179. }
  180. }
  181. #dhcp config
  182. if ($gate->{dhcp}) {
  183. #все сети роутера, которые к нему подключены напрямую фактически - Patricia Object
  184. my $fact_connected_nets = new Net::Patricia;
  185. #интерфейсы, которые будут использоваться для конфигурирования dhcp-сервера
  186. my @work_int=();
  187. #dhcp-сервер, обрабатывающий запросы от dhcp-relay
  188. my @relayed_dhcp = netdev_cmd($gate,$t,"/ip dhcp-server print terse without-paging where relay=255.255.255.255",1);
  189. my $relayed_dhcp_server;
  190. my $relayed_dhcp_interface;
  191. if (@relayed_dhcp and scalar @relayed_dhcp) {
  192. my $dhcp_server = $relayed_dhcp[0];
  193. if ($dhcp_server and $dhcp_server=~/name=(\S+)\s+/i) { $relayed_dhcp_server = $1; }
  194. if ($dhcp_server and $dhcp_server=~/interface=(\S+)\s+/i) { $relayed_dhcp_interface= $1; }
  195. }
  196. #ищем интерфейсы, на которых поднята необходимая для dhcp сервера сеть
  197. foreach my $int (@lan_int) { #interface loop
  198. next if (!$int);
  199. $int=trim($int);
  200. #get ip addr at interface
  201. my @int_addr=netdev_cmd($gate,$t,'/ip address print terse without-paging where interface='.$int,1);
  202. log_debug($gate_ident."Get interfaces: ".Dumper(\@int_addr));
  203. my $found_subnet;
  204. foreach my $int_str(@int_addr) {
  205. $int_str=trim($int_str);
  206. next if (!$int_str);
  207. if ($int_str=~/\s+address=(\S*)\s+/i) {
  208. my $gate_interface=$1;
  209. if ($gate_interface) {
  210. my $gate_ip=$gate_interface;
  211. my $gate_net = GetDhcpRange($gate_interface);
  212. $fact_connected_nets->add_string($gate_net->{network}."/".$gate_net->{masklen});
  213. $gate_ip=~s/\/.*$//;
  214. #search for first match
  215. $found_subnet=$dhcp_networks->match_string($gate_ip);
  216. last;
  217. }
  218. }
  219. }
  220. if (!$found_subnet) { db_log_verbose($dbh,$gate_ident."DHCP subnet for interface $int not found! Skip interface."); next; }
  221. my $dhcp_state;
  222. $dhcp_state->{subnet}=$found_subnet;
  223. $dhcp_state->{interface}=$int;
  224. #формируем список локальных интерфейсов, на котором есть dhcp-сеть
  225. push(@work_int,$dhcp_state);
  226. }
  227. #формируем список сетей, не подключенных к роутеру непосредтственно
  228. my @relayed_subnets=();
  229. foreach my $gw_subnet (keys %connected_nets_hash ) {
  230. next if (!$gw_subnet);
  231. next if ($fact_connected_nets->match_string($gw_subnet));
  232. push(@relayed_subnets,$gw_subnet);
  233. }
  234. if (scalar @relayed_subnets and $relayed_dhcp_interface) {
  235. my $dhcp_state;
  236. $dhcp_state->{interface} = $relayed_dhcp_interface;
  237. $dhcp_state->{subnet} = join(",",@relayed_subnets);
  238. push(@work_int,$dhcp_state);
  239. }
  240. #interface dhcp loop
  241. foreach my $dhcpd_int (@work_int) {
  242. my $found_subnet=$dhcpd_int->{subnet};
  243. my @dhcp_subnets = split(/\,/,$found_subnet);
  244. my $int=$dhcpd_int->{interface};
  245. db_log_verbose($dbh,$gate_ident."Analyze interface $int. Found: ".Dumper($dhcp_conf{$found_subnet}));
  246. #fetch current dhcp records
  247. my @ret_static_leases=netdev_cmd($gate,$t,'/ip dhcp-server lease print terse without-paging where server=dhcp-'.$int,1);
  248. log_debug($gate_ident."Get dhcp leases:".Dumper(\@ret_static_leases));
  249. my @current_static_leases=();
  250. foreach my $str (@ret_static_leases) {
  251. next if (!$str);
  252. $str=trim($str);
  253. if ($str=~/^\d/) {
  254. log_debug($gate_ident."Found current static lease record: ".$str);
  255. push(@current_static_leases,$str);
  256. }
  257. }
  258. #select users for this interface
  259. my @auth_records=();
  260. foreach my $dhcp_subnet (@dhcp_subnets) {
  261. next if (!$dhcp_subnet);
  262. next if (!exists $dhcp_conf{$dhcp_subnet});
  263. my $a_sql = "SELECT * FROM user_auth WHERE deleted = 0 AND dhcp = 1 AND ip_int BETWEEN ? AND ? AND ou_id NOT IN (?, ?) ORDER BY ip_int";
  264. my @tmp1=get_records_sql($dbh,$a_sql,$dhcp_conf{$dhcp_subnet}->{first_ip_aton},$dhcp_conf{$dhcp_subnet}->{last_ip_aton},$default_user_ou_id, $default_hotspot_ou_id);
  265. push(@auth_records,@tmp1);
  266. undef @tmp1;
  267. }
  268. my %leases;
  269. foreach my $lease (@auth_records) {
  270. next if (!$lease);
  271. next if (!$lease->{mac});
  272. next if (!$lease->{ip});
  273. my $found_subnet = $dhcp_networks->match_string($lease->{ip});
  274. next if (!$found_subnet);
  275. next if ($lease->{ip} eq $dhcp_conf{$found_subnet}->{relay_ip});
  276. $leases{$lease->{ip}}{ip}=$lease->{ip};
  277. $leases{$lease->{ip}}{description}=$lease->{id};
  278. $leases{$lease->{ip}}{id}=$lease->{id};
  279. $leases{$lease->{ip}}{dns_name}=$lease->{dns_name};
  280. if ($lease->{description}) { $leases{$lease->{ip}}{description}=translit($lease->{description}); }
  281. $leases{$lease->{ip}}{mac}=uc(mac_splitted($lease->{mac}));
  282. if ($lease->{dhcp_acl}) {
  283. $leases{$lease->{ip}}{acl}=trim($lease->{dhcp_acl});
  284. $leases{$lease->{ip}}{acl}=~s/;/,/g;
  285. if ($leases{$lease->{ip}}{acl}=~/hotspot\-free/) {
  286. $hotspot_exceptions{$leases{$lease->{ip}}{mac}}=$leases{$lease->{ip}}{mac};
  287. $hotspot_exceptions{$leases{$lease->{ip}}{mac}}=$leases{$lease->{ip}}{description} if ($leases{$lease->{ip}}{description});
  288. }
  289. }
  290. if ($lease->{dhcp_option_set}) {
  291. $leases{$lease->{ip}}{dhcp_option_set}=trim($lease->{dhcp_option_set});
  292. }
  293. $leases{$lease->{ip}}{acl}='' if (!$leases{$lease->{ip}}{acl});
  294. $leases{$lease->{ip}}{dhcp_option_set}='' if (!$leases{$lease->{ip}}{dhcp_option_set});
  295. }
  296. my %active_leases;
  297. foreach my $lease (@current_static_leases) {
  298. my @words = split(/\s+/,$lease);
  299. my %tmp_lease;
  300. if ($lease=~/^(\d*)\s+/) { $tmp_lease{id}=$1; };
  301. next if (!defined($tmp_lease{id}));
  302. foreach my $option (@words) {
  303. next if (!$option);
  304. $option=trim($option);
  305. next if (!$option);
  306. my @tmp = split(/\=/,$option);
  307. my $token = trim($tmp[0]);
  308. my $value = trim($tmp[1]);
  309. next if (!$token);
  310. next if (!$value);
  311. $value=~s/\"//g;
  312. if ($token=~/^address$/i) { $tmp_lease{ip}=GetIP($value); }
  313. if ($token=~/^mac-address$/i) { $tmp_lease{mac}=uc(mac_splitted($value)); }
  314. if ($token=~/^address-lists$/i) { $tmp_lease{acl}=$value; }
  315. if ($token=~/^dhcp-option-set$/i) { $tmp_lease{dhcp_option_set}=$value; }
  316. }
  317. next if (!$tmp_lease{ip});
  318. next if (!$tmp_lease{mac});
  319. next if ($lease=~/^(\d*)\s+D\s+/);
  320. $active_leases{$tmp_lease{ip}}{ip}=$tmp_lease{ip};
  321. $active_leases{$tmp_lease{ip}}{mac}=$tmp_lease{mac};
  322. $active_leases{$tmp_lease{ip}}{id}=$tmp_lease{id};
  323. $active_leases{$tmp_lease{ip}}{acl}='';
  324. $active_leases{$tmp_lease{ip}}{dhcp_option_set}='';
  325. if ($tmp_lease{acl}) {
  326. $active_leases{$tmp_lease{ip}}{acl}=$tmp_lease{acl};
  327. }
  328. if ($tmp_lease{dhcp_option_set}) {
  329. $active_leases{$tmp_lease{ip}}{dhcp_option_set}=$tmp_lease{dhcp_option_set};
  330. }
  331. }
  332. log_debug($gate_ident."Active leases: ".Dumper(\%active_leases));
  333. #sync state
  334. foreach my $ip (keys %active_leases) {
  335. if (!exists $leases{$ip}) {
  336. db_log_verbose($dbh,$gate_ident."Address $ip not found in stat. Remove from router.");
  337. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  338. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  339. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  340. next;
  341. }
  342. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  343. db_log_verbose($dbh,$gate_ident."Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Remove lease from router.");
  344. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  345. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  346. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  347. next;
  348. }
  349. if (!(!$leases{$ip}{acl} and !$active_leases{$ip}{acl}) and $leases{$ip}{acl} ne $active_leases{$ip}{acl}) {
  350. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Remove lease from router.");
  351. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  352. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  353. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  354. next;
  355. }
  356. if (!(!$leases{$ip}{dhcp_option_set} and !$active_leases{$ip}{dhcp_option_set}) and $leases{$ip}{dhcp_option_set} ne $active_leases{$ip}{dhcp_option_set}) {
  357. db_log_error($dbh,$gate_ident."DHCP option-set mismatch for ip $ip. stat: $leases{$ip}{dhcp_option_set} active: $active_leases{$ip}{dhcp_option_set}. Remove lease from router.");
  358. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  359. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  360. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  361. next;
  362. }
  363. }
  364. foreach my $ip (keys %leases) {
  365. my $acl='';
  366. if ($leases{$ip}{acl}) { $acl = 'address-lists='.$leases{$ip}{acl}; }
  367. my $dhcp_option_set='';
  368. if ($leases{$ip}{dhcp_option_set}) { $dhcp_option_set = 'dhcp-option-set='.$leases{$ip}{dhcp_option_set}; }
  369. my $description = $leases{$ip}{description};
  370. $description =~s/\=//g;
  371. my $dns_name='';
  372. if ($leases{$ip}{dns_name}) { $dns_name = $leases{$ip}{dns_name}; }
  373. $dns_name =~s/\=//g;
  374. if ($dns_name) { $description = 'comment="'.$dns_name." - ".$description.'"'; } else { $description = 'comment="'.$description.'"'; }
  375. if (!exists $active_leases{$ip}) {
  376. db_log_verbose($dbh,$gate_ident."Address $ip not found in router. Create static lease record.");
  377. #remove static and dynamic records for mac
  378. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  379. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  380. #remove current ip binding
  381. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  382. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  383. #add new bind
  384. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$description);
  385. #clear arp record
  386. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  387. next;
  388. }
  389. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  390. db_log_error($dbh,$gate_ident."Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Create static lease record.");
  391. #remove static and dynamic records for mac
  392. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  393. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  394. #remove current ip binding
  395. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  396. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  397. #add new bind
  398. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$description);
  399. #clear arp record
  400. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  401. next;
  402. }
  403. if (!(!$leases{$ip}{acl} and !$active_leases{$ip}{acl}) and $leases{$ip}{acl} ne $active_leases{$ip}{acl}) {
  404. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Create static lease record.");
  405. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  406. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  407. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$description);
  408. #clear arp record
  409. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  410. next;
  411. }
  412. if (!(!$leases{$ip}{dhcp_option_set} and !$active_leases{$ip}{dhcp_option_set}) and $leases{$ip}{dhcp_option_set} ne $active_leases{$ip}{dhcp_option_set}) {
  413. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Create static lease record.");
  414. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  415. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  416. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$description);
  417. #clear arp record
  418. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  419. next;
  420. }
  421. }
  422. }#end interface dhcp loop
  423. #check hotspot
  424. my @ret_hotspot = netdev_cmd($gate,$t,'/ip hotspot print terse where disabled=no',1);
  425. #if hotspot found - apply exception
  426. if (@ret_hotspot and scalar(@ret_hotspot)) {
  427. #hotspot exceptions
  428. my @ret_hotspot_bindings=netdev_cmd($gate,$t,'/ip hotspot ip-binding print terse without-paging where type=bypassed',1);
  429. my %actual_hotspot_bindings;
  430. foreach my $row (@ret_hotspot_bindings) {
  431. next if (!$row or $row !~ /^\s*\d/);
  432. my %data;
  433. # Используем регулярное выражение для извлечения пар ключ=значение
  434. while ($row =~/\b(\S+?)=([^\s=]+(?:\s(?!\S+=)[^\s=]+)*)/g) {
  435. my ($key, $value) = ($1, $2);
  436. $data{$key} = $value;
  437. }
  438. if (exists $data{'mac-address'}) {
  439. $actual_hotspot_bindings{$data{'mac-address'}} = $data{'mac-address'};
  440. $actual_hotspot_bindings{$data{'mac-address'}} = $data{description} if (exists $data{description});
  441. }
  442. }
  443. log_debug("Hotspot actual bindings:".Dumper(\%actual_hotspot_bindings));
  444. log_debug("Hotspot configuration exceptions:".Dumper(\%hotspot_exceptions));
  445. #update binding
  446. foreach my $actual_mac (keys %actual_hotspot_bindings) {
  447. if (!exists $hotspot_exceptions{$actual_mac}) {
  448. db_log_info($dbh,$gate_ident."Address $actual_mac removed from hotspot ip-binding");
  449. push(@cmd_list,':foreach i in [/ip hotspot ip-binding find where mac-address='.uc($actual_mac).' ] do={/ip hotspot ip-binding remove $i};');
  450. }
  451. }
  452. foreach my $actual_mac (keys %hotspot_exceptions) {
  453. if (!exists $actual_hotspot_bindings{$actual_mac}) {
  454. db_log_info($dbh,$gate_ident."Address $actual_mac added to hotspot ip-binding");
  455. push(@cmd_list,':foreach i in [/ip hotspot ip-binding find where mac-address='.uc($actual_mac).' ] do={/ip hotspot ip-binding remove $i};');
  456. push(@cmd_list,'/ip hotspot ip-binding add mac-address='.uc($actual_mac).' type=bypassed comment="'.$hotspot_exceptions{$actual_mac}.'"');
  457. }
  458. }
  459. }
  460. }#end dhcp config
  461. my %users;
  462. my %lists;
  463. my $group_sql = "SELECT DISTINCT filter_group_id FROM user_auth WHERE deleted = 0 ORDER BY filter_group_id;";
  464. my @grouplist_ref = get_records_sql($dbh,$group_sql);
  465. foreach my $row (@grouplist_ref) {
  466. $lists{'group_'.$row->{filter_group_id}}=1;
  467. }
  468. #full ip list
  469. $lists{'group_all'}=1;
  470. #access lists config
  471. if ($gate->{user_acl}) {
  472. db_log_verbose($dbh,$gate_ident."Sync user state at router $router_name [".$router_ip."] started.");
  473. #get userid list
  474. my $user_auth_sql="SELECT user_auth.ip, user_auth.filter_group_id, user_auth.id
  475. FROM user_auth, user_list
  476. WHERE user_auth.user_id = user_list.id
  477. AND user_auth.deleted =0
  478. AND user_auth.enabled =1
  479. AND user_auth.blocked =0
  480. AND user_list.blocked =0
  481. AND user_list.enabled =1
  482. AND user_auth.ou_id <> ?
  483. ORDER BY ip_int";
  484. my @authlist_ref = get_records_sql($dbh,$user_auth_sql,$default_hotspot_ou_id);
  485. foreach my $row (@authlist_ref) {
  486. if ($connected_users_only) { next if (!$connected_users->match_string($row->{ip})); }
  487. #skip not office ip's
  488. next if (!$office_networks->match_string($row->{ip}));
  489. #filter group acl's
  490. $users{'group_'.$row->{filter_group_id}}->{$row->{ip}}=1;
  491. $users{'group_all'}->{$row->{ip}}=1;
  492. }
  493. log_debug($gate_ident."Users status by ACL:".Dumper(\%users));
  494. my @filter_instances = get_records_sql($dbh,"SELECT * FROM filter_instances");
  495. my @filterlist_ref = get_records_sql($dbh,"SELECT * FROM filter_list where filter_type=0");
  496. my %filters;
  497. my %dyn_filters;
  498. my $max_filter_rec = get_record_sql($dbh,"SELECT MAX(id) as max_filter FROM filter_list");
  499. my $max_filter_id = $max_filter_rec->{max_filter};
  500. my $dyn_filters_base = $max_filter_id+1000;
  501. my $dyn_filters_index = $dyn_filters_base;
  502. foreach my $row (@filterlist_ref) {
  503. #if dst - ip address
  504. if (is_ip($row->{dst})) {
  505. $filters{$row->{id}}->{id}=$row->{id};
  506. $filters{$row->{id}}->{proto}=$row->{proto};
  507. $filters{$row->{id}}->{dst}=$row->{dst};
  508. $filters{$row->{id}}->{dstport}=$row->{dstport};
  509. $filters{$row->{id}}->{srcport}=$row->{srcport};
  510. #set false for dns dst flag
  511. $filters{$row->{id}}->{dns_dst}=0;
  512. } else {
  513. #if dst not ip - check dns record
  514. my @dns_record=ResolveNames($row->{dst},undef);
  515. my $resolved_ips = (scalar @dns_record>0);
  516. next if (!$resolved_ips);
  517. foreach my $resolved_ip (sort @dns_record) {
  518. next if (!$resolved_ip);
  519. #enable dns dst filters
  520. $filters{$row->{id}}->{dns_dst}=1;
  521. #add dynamic dns filter
  522. $filters{$dyn_filters_index}->{id}=$row->{id};
  523. $filters{$dyn_filters_index}->{proto}=$row->{proto};
  524. $filters{$dyn_filters_index}->{dst}=$resolved_ip;
  525. $filters{$dyn_filters_index}->{dstport}=$row->{dstport};
  526. $filters{$dyn_filters_index}->{srcport}=$row->{srcport};
  527. $filters{$dyn_filters_index}->{dns_dst}=0;
  528. #save new filter dns id for original filter id
  529. push(@{$dyn_filters{$row->{id}}},$dyn_filters_index);
  530. $dyn_filters_index++;
  531. }
  532. }
  533. }
  534. log_debug($gate_ident."Filters status:". Dumper(\%filters));
  535. log_debug($gate_ident."DNS-filters status:". Dumper(\%dyn_filters));
  536. #clean unused filter records
  537. do_sql($dbh,"DELETE FROM group_filters WHERE group_id NOT IN (SELECT id FROM group_list)");
  538. do_sql($dbh,"DELETE FROM group_filters WHERE filter_id NOT IN (SELECT id FROM filter_list)");
  539. my @groups_list = get_records_sql($dbh,"SELECT * FROM group_list");
  540. my %groups;
  541. foreach my $group (@groups_list) { $groups{'group_'.$group->{id}}=$group; }
  542. my @grouplist_ref = get_records_sql($dbh,"SELECT group_id,filter_id,rule_order,action FROM group_filters ORDER BY group_filters.group_id,group_filters.rule_order");
  543. my %group_filters;
  544. my $index = 0;
  545. my $cur_group;
  546. foreach my $row (@grouplist_ref) {
  547. if (!$cur_group) { $cur_group = $row->{group_id}; }
  548. if ($cur_group != $row->{group_id}) {
  549. $index = 0;
  550. $cur_group = $row->{group_id};
  551. }
  552. #if dst dns filter not found
  553. if (!$filters{$row->{filter_id}}->{dns_dst}) {
  554. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$row->{filter_id};
  555. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  556. $index++;
  557. } else {
  558. #if found dns dst filters - add
  559. if (exists $dyn_filters{$row->{filter_id}}) {
  560. my @dyn_ips = @{$dyn_filters{$row->{filter_id}}};
  561. if (scalar @dyn_ips >0) {
  562. for (my $i = 0; $i < scalar @dyn_ips; $i++) {
  563. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$dyn_ips[$i];
  564. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  565. $index++;
  566. }
  567. }
  568. }
  569. }
  570. }
  571. log_debug($gate_ident."Group filters: ".Dumper(\%group_filters));
  572. #sync firewall rules
  573. #sync group chains
  574. foreach my $filter_instance (@filter_instances) {
  575. my $instance_name = 'Users';
  576. if ($filter_instance->{id}>1) {
  577. $instance_name = 'Users-'.$filter_instance->{name};
  578. #check filter instance exist at gateway
  579. my $instance_ok = get_record_sql($dbh,"SELECT * FROM device_filter_instances WHERE device_id= ? AND instance_id=?", $gate->{'id'}, $filter_instance->{id});
  580. #skip insatnce if not found
  581. if (!$instance_ok) { next; }
  582. }
  583. my @chain_list=netdev_cmd($gate,$t,'/ip firewall filter print terse without-paging where chain='.$instance_name.' and action=jump',1);
  584. log_debug($gate_ident."Get firewall chains:".Dumper(\@chain_list));
  585. my %cur_chain;
  586. foreach my $jump_list (@chain_list) {
  587. next if (!$jump_list);
  588. $jump_list=trim($jump_list);
  589. if ($jump_list=~/jump-target=(\S*)\s+/i) {
  590. if ($1) { $cur_chain{$1}++; }
  591. }
  592. }
  593. #old chains
  594. foreach my $group_name (keys %cur_chain) {
  595. if (!exists($group_filters{$group_name}) or $groups{$group_name}->{instance_id} ne $filter_instance->{id}) {
  596. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=".$instance_name." and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  597. } else {
  598. if ($cur_chain{$group_name} != 2) {
  599. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=".$instance_name." and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  600. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." src-address-list=".$group_name);
  601. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  602. }
  603. }
  604. }
  605. #new chains
  606. foreach my $group_name (keys %group_filters) {
  607. if (!exists($cur_chain{$group_name}) and $groups{$group_name}->{instance_id} eq $filter_instance->{id}) {
  608. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." src-address-list=".$group_name);
  609. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  610. }
  611. }
  612. }
  613. my %chain_rules;
  614. foreach my $group_name (sort keys %group_filters) {
  615. next if (!$group_name);
  616. next if (!exists($group_filters{$group_name}));
  617. my %group_filter = %{$group_filters{$group_name}};
  618. foreach my $filter_index (sort keys %group_filter) {
  619. my $filter = $group_filter{$filter_index};
  620. my $filter_id=$filter->{filter_id};
  621. next if (!$filters{$filter_id});
  622. next if ($filters{$filter_id}->{dns_dst});
  623. my $src_rule='chain='.$group_name;
  624. my $dst_rule='chain='.$group_name;
  625. if ($filter->{action}) {
  626. $src_rule=$src_rule." action=accept";
  627. $dst_rule=$dst_rule." action=accept";
  628. } else {
  629. $src_rule=$src_rule." action=reject";
  630. $dst_rule=$dst_rule." action=reject";
  631. }
  632. if ($filters{$filter_id}->{proto} and ($filters{$filter_id}->{proto}!~/all/i)) {
  633. $src_rule=$src_rule." protocol=".$filters{$filter_id}->{proto};
  634. $dst_rule=$dst_rule." protocol=".$filters{$filter_id}->{proto};
  635. }
  636. if ($filters{$filter_id}->{dst} and $filters{$filter_id}->{dst} ne '0/0') {
  637. $src_rule=$src_rule." src-address=".trim($filters{$filter_id}->{dst});
  638. $dst_rule=$dst_rule." dst-address=".trim($filters{$filter_id}->{dst});
  639. }
  640. #dstport and srcport
  641. if (!$filters{$filter_id}->{dstport}) { $filters{$filter_id}->{dstport}=0; }
  642. if (!$filters{$filter_id}->{srcport}) { $filters{$filter_id}->{srcport}=0; }
  643. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} ne '0') {
  644. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport})." src-port=".trim($filters{$filter_id}->{dstport});
  645. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport})." dst-port=".trim($filters{$filter_id}->{dstport});
  646. }
  647. if ($filters{$filter_id}->{dstport} eq '0' and $filters{$filter_id}->{srcport} ne '0') {
  648. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport});
  649. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport});
  650. }
  651. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} eq '0') {
  652. $src_rule=$src_rule." src-port=".trim($filters{$filter_id}->{dstport});
  653. $dst_rule=$dst_rule." dst-port=".trim($filters{$filter_id}->{dstport});
  654. }
  655. if ($src_rule ne $dst_rule) {
  656. push(@{$chain_rules{$group_name}},$src_rule);
  657. push(@{$chain_rules{$group_name}},$dst_rule);
  658. } else {
  659. push(@{$chain_rules{$group_name}},$src_rule);
  660. }
  661. }
  662. }
  663. #chain filters
  664. foreach my $group_name (sort keys %group_filters) {
  665. next if (!$group_name);
  666. my @get_filter=netdev_cmd($gate,$t,'/ip firewall filter print terse without-paging where chain='.$group_name,1);
  667. chomp(@get_filter);
  668. my @cur_filter=();
  669. my $chain_ok=1;
  670. foreach (my $f_index=0; $f_index<scalar(@get_filter); $f_index++) {
  671. my $filter_str=trim($get_filter[$f_index]);
  672. next if (!$filter_str);
  673. next if ($filter_str!~/^(\d){1,3}/);
  674. $filter_str=~s/[^[:ascii:]]//g;
  675. $filter_str=~s/^\d{1,3}\s+//;
  676. $filter_str=trim($filter_str);
  677. next if (!$filter_str);
  678. push(@cur_filter,$filter_str);
  679. }
  680. log_debug($gate_ident."Current filters:".Dumper(\@cur_filter));
  681. log_debug($gate_ident."New filters:".Dumper($chain_rules{$group_name}));
  682. #current state rules
  683. foreach (my $f_index=0; $f_index<scalar(@cur_filter); $f_index++) {
  684. my $filter_str=trim($cur_filter[$f_index]);
  685. if (!$chain_rules{$group_name}[$f_index] or $filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  686. print "Check chain $group_name error! $filter_str not found in new config. Recreate chain.\n";
  687. $chain_ok=0;
  688. last;
  689. }
  690. }
  691. #new rules
  692. if ($chain_ok and $chain_rules{$group_name} and scalar(@{$chain_rules{$group_name}})) {
  693. foreach (my $f_index=0; $f_index<scalar(@{$chain_rules{$group_name}}); $f_index++) {
  694. my $filter_str=trim($cur_filter[$f_index]);
  695. if (!$filter_str) {
  696. print "Check chain $group_name error! Not found: $chain_rules{$group_name}[$f_index]. Recreate chain.\n";
  697. $chain_ok=0;
  698. last;
  699. }
  700. $filter_str=~s/^\d//;
  701. $filter_str=trim($filter_str);
  702. if ($filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  703. print "Check chain $group_name error! Expected: $chain_rules{$group_name}[$f_index] Found: $filter_str. Recreate chain.\n";
  704. $chain_ok=0;
  705. last;
  706. }
  707. }
  708. }
  709. if (!$chain_ok) {
  710. push(@cmd_list,":foreach i in [/ip firewall filter find where chain=".$group_name." ] do={/ip firewall filter remove \$i};");
  711. foreach my $filter_str (@{$chain_rules{$group_name}}) {
  712. push(@cmd_list,'/ip firewall filter add '.$filter_str);
  713. }
  714. }
  715. }
  716. }#end access lists config
  717. if ($shaper_enabled) {
  718. #get userid list
  719. my $user_auth_sql="SELECT ip, queue_id, id FROM user_auth WHERE user_auth.deleted =0 AND user_auth.ou_id <> ? ORDER BY ip_int";
  720. my @authlist_ref = get_records_sql($dbh,$user_auth_sql,$default_hotspot_ou_id);
  721. foreach my $row (@authlist_ref) {
  722. if ($connected_users_only) { next if (!$connected_users->match_string($row->{ip})); }
  723. #skip not office ip's
  724. next if (!$office_networks->match_string($row->{ip}));
  725. #queue acl's
  726. if ($row->{queue_id}) { $users{'queue_'.$row->{queue_id}}->{$row->{ip}}=1; }
  727. }
  728. #get queue list
  729. my @queuelist_ref = get_records_sql($dbh,"SELECT * FROM queue_list");
  730. my %queues;
  731. foreach my $row (@queuelist_ref) {
  732. $lists{'queue_'.$row->{id}}=1;
  733. next if ((!$row->{download}) and !($row->{upload}));
  734. $queues{'queue_'.$row->{id}}{id}=$row->{id};
  735. $queues{'queue_'.$row->{id}}{down}=$row->{download};
  736. $queues{'queue_'.$row->{id}}{up}=$row->{upload};
  737. }
  738. log_debug($gate_ident."Queues status:".Dumper(\%queues));
  739. #shapers
  740. my %get_queue_root=();
  741. my %get_queue_type=();
  742. my %get_queue_tree=();
  743. my %get_filter_mangle=();
  744. my @tmp=netdev_cmd($gate,$t,'/queue tree print terse without-paging where name~"(down|up)load_root"',1);
  745. log_debug($gate_ident."Get ROOT queues classes: ".Dumper(\@tmp));
  746. #5 name=upload_root_sfp-sfpplus1-wan parent=sfp-sfpplus1-wan packet-mark= limit-at=0 queue=pcq-upload-default priority=8 max-limit=10G burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  747. #0 name=download_root_vlan0201 parent=vlan0201 packet-mark= limit-at=0 queue=pcq-download-default priority=8 max-limit=2G burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  748. foreach my $row (@tmp) {
  749. next if (!$row);
  750. $row = trim($row);
  751. next if ($row!~/^(\d){1,3}/);
  752. $row=~s/^\d{1,3}\s+//;
  753. next if (!$row);
  754. if ($row=~/name=(down|up)load_root_(\S*)\s+/i) {
  755. next if (!$2);
  756. my $parent_device = $2;
  757. $get_queue_root{$parent_device}{name} = $parent_device;
  758. if ($row=~/\s+max-limit=(\S*)\s+/) {
  759. $get_queue_root{$parent_device}{bandwidth} = bitrate_to_kbps($1);
  760. } else { $get_queue_root{$parent_device}{bandwidth} = 0; }
  761. }
  762. }
  763. #log_debug($gate_ident.'GET ROOT:'.Dumper(\%get_queue_root));
  764. @tmp=();
  765. @tmp=netdev_cmd($gate,$t,'/queue type print terse without-paging where name~"pcq_(down|up)load"',1);
  766. log_debug($gate_ident."Get queues: ".Dumper(\@tmp));
  767. # 0 name=pcq_upload_3 kind=pcq pcq-rate=102401k pcq-limit=500KiB pcq-classifier=src-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s
  768. #pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64
  769. foreach my $row (@tmp) {
  770. next if (!$row);
  771. $row = trim($row);
  772. next if ($row!~/^(\d){1,3}/);
  773. $row=~s/^\d{1,3}\s+//;
  774. next if (!$row);
  775. if ($row=~/name=pcq_(down|up)load_(\d){1,3}\s+/i) {
  776. next if (!$1);
  777. next if (!$2);
  778. my $direct = $1;
  779. my $index = $2;
  780. $get_queue_type{$index}{$direct}=$row;
  781. if ($row=~/pcq-rate=(\S*)\s+\S/i) {
  782. my $rate = $1;
  783. if ($rate=~/k$/i) { $rate =~s/k$//i; }
  784. $get_queue_type{$index}{$direct."-rate"}=$rate;
  785. }
  786. if ($row=~/pcq-classifier=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-classifier"}=$1; }
  787. if ($row=~/pcq-src-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-src-address-mask"}=$1; }
  788. if ($row=~/pcq-dst-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-dst-address-mask"}=$1; }
  789. }
  790. }
  791. @tmp=();
  792. @tmp=netdev_cmd($gate,$t,'/queue tree print terse without-paging where parent~"(download|upload)_root"',1);
  793. #log_debug($gate_ident."Get user queues: ".Dumper(\@tmp));
  794. #print Dumper(\@tmp);
  795. # 0 I name=queue_3_out parent=upload_root packet-mark=upload_3 limit-at=0 queue=*2A priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  796. # 5 I name=queue_3_vlan2_in parent=download_root_vlan2 packet-mark=download_3_vlan2 limit-at=0 queue=*2B priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  797. foreach my $row (@tmp) {
  798. next if (!$row);
  799. $row = trim($row);
  800. next if ($row!~/^(\d)/);
  801. $row=~s/^(\d*)\s+//;
  802. next if (!$row);
  803. if ($row=~/queue=pcq_(down|up)load_(\d){1,3}/i) {
  804. if ($row=~/name=queue_(\d){1,3}_(\S*)_out\s+/i) {
  805. next if (!$1);
  806. next if (!$2);
  807. my $index = $1;
  808. my $int_name = $2;
  809. $get_queue_tree{$index}{$int_name}{up}=$row;
  810. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-parent'}=$1; }
  811. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-mark'}=$1; }
  812. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-queue'}=$1; }
  813. }
  814. if ($row=~/name=queue_(\d){1,3}_(\S*)_in\s+/i) {
  815. next if (!$1);
  816. next if (!$2);
  817. my $index = $1;
  818. my $int_name = $2;
  819. $get_queue_tree{$index}{$int_name}{down}=$row;
  820. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-parent'}=$1; }
  821. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-mark'}=$1; }
  822. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-queue'}=$1; }
  823. }
  824. }
  825. }
  826. @tmp=();
  827. @tmp=netdev_cmd($gate,$t,'/ip firewall mangle print terse without-paging where action=mark-packet and new-packet-mark~"(upload|download)_[0-9]{1,3}"',1);
  828. log_debug($gate_ident."Get firewall mangle rules for queues:".Dumper(\@tmp));
  829. # 0 chain=forward action=mark-packet new-packet-mark=upload_0 passthrough=yes src-address-list=queue_0 out-interface=sfp-sfpplus1-wan log=no log-prefix=""
  830. # 0 chain=forward action=mark-packet new-packet-mark=download_3_vlan2 passthrough=yes dst-address-list=queue_3 out-interface=vlan2 in-interface-list=WAN log=no log-prefix=""
  831. foreach my $row (@tmp) {
  832. next if (!$row);
  833. $row = trim($row);
  834. next if ($row!~/^(\d){1,3}/);
  835. $row=~s/^\d{1,3}\s+//;
  836. next if (!$row);
  837. if ($row=~/new-packet-mark=upload_(\d){1,3}_(\S*)\s+/i) {
  838. next if (!$1);
  839. next if (!$2);
  840. my $index = $1;
  841. my $int_name = $2;
  842. $get_filter_mangle{$index}{$int_name}{up}=$row;
  843. if ($row=~/src-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-list'}=$1; }
  844. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-dev'}=$1; }
  845. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-mark'}=$1; }
  846. }
  847. if ($row=~/new-packet-mark=download_(\d){1,3}_(\S*)\s+/i) {
  848. next if (!$1);
  849. next if (!$2);
  850. my $index = $1;
  851. my $int_name = $2;
  852. $get_filter_mangle{$index}{$int_name}{down}=$row;
  853. if ($row=~/dst-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-list'}=$1; }
  854. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-mark'}=$1; }
  855. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-dev'}=$1; }
  856. }
  857. }
  858. log_debug($gate_ident."Queues root classes:".Dumper(\%get_queue_root));
  859. log_debug($gate_ident."Queues type status:".Dumper(\%get_queue_type));
  860. log_debug($gate_ident."Queues tree status:".Dumper(\%get_queue_tree));
  861. log_debug($gate_ident."Firewall mangle status:".Dumper(\%get_filter_mangle));
  862. my %queue_type;
  863. my %queue_tree;
  864. my %filter_mangle;
  865. #analyze root class
  866. foreach my $l3_int (keys %l3_interfaces) {
  867. my $int_type = 'download';
  868. if ($l3_interfaces{$l3_int}->{type}) { $int_type = 'upload'; }
  869. #clear unknown root queue
  870. push(@cmd_list,'/queue tree remove [ find name!~"'.$int_type . '_root_' . $l3_int .'" and parent='.$l3_int.' ]');
  871. # add root queue if not exists
  872. if (!exists $get_queue_root{$l3_int}) {
  873. push(@cmd_list,'/queue tree add max-limit=' . kbps_to_bitrate($l3_interfaces{$l3_int}->{bandwidth}) . ' name=' . $int_type . '_root_' . $l3_int .' parent=' . $l3_int . ' queue=pcq-' . $int_type . '-default');
  874. next;
  875. }
  876. # change bandwidth if differs
  877. if ($get_queue_root{$l3_int}{bandwidth} ne $l3_interfaces{$l3_int}->{bandwidth}) {
  878. push(@cmd_list,'/queue tree set max-limit=' . kbps_to_bitrate($l3_interfaces{$l3_int}->{bandwidth}) . ' [ find name='.$int_type . '_root_' . $l3_int . ' ]');
  879. next;
  880. }
  881. }
  882. #generate new config
  883. foreach my $queue_name (keys %queues) {
  884. my $q_id=$queues{$queue_name}{id};
  885. my $q_up=$queues{$queue_name}{up};
  886. my $q_down=$queues{$queue_name}{down};
  887. #queue_types
  888. $queue_type{$q_id}{up}="name=pcq_upload_".$q_id." kind=pcq pcq-rate=".$q_up."k pcq-limit=500KiB pcq-classifier=src-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64";
  889. $queue_type{$q_id}{down}="name=pcq_download_".$q_id." kind=pcq pcq-rate=".$q_down."k pcq-limit=500KiB pcq-classifier=dst-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64";
  890. my $queue_ok=1;
  891. if (!$get_queue_type{$q_id}{up}) { $queue_ok=0; }
  892. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'up-rate'})>10) { $queue_ok=0; }
  893. if ($queue_ok and $get_queue_type{$q_id}{'up-classifier'}!~/src-address/i) { $queue_ok=0; }
  894. if (!$queue_ok) {
  895. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_upload_'.$q_id.'" ] do={/queue type remove $i};');
  896. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{up});
  897. }
  898. $queue_ok=1;
  899. if (!$get_queue_type{$q_id}{down}) { $queue_ok=0; }
  900. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'down-rate'})>10) { $queue_ok=0; }
  901. if ($queue_ok and $get_queue_type{$q_id}{'down-classifier'}!~/dst-address/i) { $queue_ok=0; }
  902. if (!$queue_ok) {
  903. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_download_'.$q_id.'" ] do={/queue type remove $i};');
  904. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{down});
  905. }
  906. #upload queue
  907. foreach my $int (@wan_int) {
  908. $queue_tree{$q_id}{$int}{up}="name=queue_".$q_id."_".$int."_out parent=upload_root_".$int." packet-mark=upload_".$q_id."_".$int." limit-at=0 queue=pcq_upload_".$q_id." priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1";
  909. $filter_mangle{$q_id}{$int}{up}="chain=forward action=mark-packet new-packet-mark=upload_".$q_id."_".$int." passthrough=yes src-address-list=queue_".$q_id." out-interface=".$int." log=no log-prefix=\"\"";
  910. $queue_ok=1;
  911. if (!$get_queue_tree{$q_id}{$int}{up}) { $queue_ok=0; }
  912. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-parent'} ne "upload_root_".$int)) { $queue_ok=0;}
  913. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  914. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-queue'} ne "pcq_upload_".$q_id)) { $queue_ok=0; }
  915. if (!$queue_ok) {
  916. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_out".'" ] do={/queue tree remove $i};');
  917. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{up});
  918. }
  919. $queue_ok=1;
  920. if (!$get_filter_mangle{$q_id}{$int}{up}) { $queue_ok=0; }
  921. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  922. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  923. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-dev'} ne $int)) { $queue_ok=0; }
  924. if (!$queue_ok) {
  925. push(@cmd_list,':foreach i in [/ip firewall mangle find where action=mark-packet and new-packet-mark~"upload_'.$q_id."_".$int.'" ] do={/ip firewall mangle remove $i};');
  926. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{up});
  927. }
  928. }
  929. #download
  930. foreach my $int (@lan_int) {
  931. next if (!$int);
  932. $queue_tree{$q_id}{$int}{down}="name=queue_".$q_id."_".$int."_in parent=download_root_".$int." packet-mark=download_".$q_id."_".$int." limit-at=0 queue=pcq_download_".$q_id." priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1";
  933. $filter_mangle{$q_id}{$int}{down}="chain=forward action=mark-packet new-packet-mark=download_".$q_id."_".$int." passthrough=yes dst-address-list=queue_".$q_id." out-interface=".$int." in-interface-list=WAN log=no log-prefix=\"\"";
  934. $queue_ok=1;
  935. if (!$get_queue_tree{$q_id}{$int}{down}) { $queue_ok=0; }
  936. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-parent'} ne "download_root_".$int)) { $queue_ok=0; }
  937. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  938. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-queue'} ne "pcq_download_".$q_id)) { $queue_ok=0; }
  939. if (!$queue_ok) {
  940. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_in".'" ] do={/queue tree remove $i};');
  941. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{down});
  942. }
  943. $queue_ok=1;
  944. if (!$get_filter_mangle{$q_id}{$int}{down}) { $queue_ok=0; }
  945. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  946. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  947. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-dev'} ne $int)) { $queue_ok=0; }
  948. if (!$queue_ok) {
  949. push(@cmd_list,':foreach i in [/ip firewall mangle find where action=mark-packet and new-packet-mark~"download_'.$q_id."_".$int.'" ] do={/ip firewall mangle remove $i};');
  950. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{down});
  951. }
  952. }
  953. }
  954. }#end shaper
  955. # Analyze actual ACL
  956. my %cur_users;
  957. foreach my $group_name (keys %lists) {
  958. my @address_lists=netdev_cmd($gate,$t,'/ip firewall address-list print terse without-paging where list='.$group_name,1);
  959. log_debug($gate_ident."Get address lists:".Dumper(\@address_lists));
  960. foreach my $row (@address_lists) {
  961. $row=trim($row);
  962. next if (!$row);
  963. my @address=split(' ',$row);
  964. foreach my $row (@address) {
  965. if ($row=~/address\=(.*)/i) { $cur_users{$group_name}{$1}=1; }
  966. }
  967. }
  968. }
  969. #new-ips
  970. foreach my $group_name (keys %users) {
  971. foreach my $user_ip (keys %{$users{$group_name}}) {
  972. if (!exists($cur_users{$group_name}{$user_ip})) {
  973. db_log_verbose($dbh,$gate_ident."Add user with ip: $user_ip to access-list $group_name");
  974. push(@cmd_list,"/ip firewall address-list add address=".$user_ip." list=".$group_name);
  975. }
  976. }
  977. }
  978. #old-ips
  979. foreach my $group_name (keys %cur_users) {
  980. foreach my $user_ip (keys %{$cur_users{$group_name}}) {
  981. if (!exists($users{$group_name}{$user_ip})) {
  982. db_log_verbose($dbh,$gate_ident."Remove user with ip: $user_ip from access-list $group_name");
  983. push(@cmd_list,":foreach i in [/ip firewall address-list find where address=".$user_ip." and list=".$group_name."] do={/ip firewall address-list remove \$i};");
  984. }
  985. }
  986. }
  987. timestamp;
  988. if (scalar(@cmd_list)) {
  989. log_debug($gate_ident."Apply:");
  990. if ($debug) { foreach my $cmd (@cmd_list) { log_debug($gate_ident."$cmd"); } }
  991. eval {
  992. netdev_cmd($gate,$t,\@cmd_list,1);
  993. };
  994. if ($@) {
  995. $all_ok = 0;
  996. log_debug($gate_ident."Error programming gateway! Err: ".$@);
  997. }
  998. }
  999. db_log_verbose($dbh,$gate_ident."Sync user state stopped.");
  1000. $dbh->disconnect();
  1001. $pm->finish;
  1002. }
  1003. $pm->wait_all_children;
  1004. #clear changed
  1005. if ($all_ok) {
  1006. foreach my $row (@changes_found) {
  1007. do_sql($dbh,"UPDATE user_auth SET changed=0 WHERE id=?",$row->{id});
  1008. }
  1009. }
  1010. if (IsMyPID($SPID)) { Remove_PID($SPID); };
  1011. do_exit 0;