sync_mikrotik.pl 60 KB

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