sync_mikrotik.pl 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use English;
  8. use base;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use strict;
  12. use Time::Local;
  13. use FileHandle;
  14. use Data::Dumper;
  15. use eyelib::config;
  16. use eyelib::main;
  17. use eyelib::cmd;
  18. use Net::Patricia;
  19. use Date::Parse;
  20. use eyelib::net_utils;
  21. use eyelib::database;
  22. use DBI;
  23. use Fcntl qw(:flock);
  24. use Parallel::ForkManager;
  25. use Net::DNS;
  26. #$debug = 1;
  27. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  28. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  29. $|=1;
  30. if (IsNotRun($SPID)) { Add_PID($SPID); } else { die "Warning!!! $SPID already runnning!\n"; }
  31. my $fork_count = $cpu_count*10;
  32. #flag for operation status
  33. my $all_ok = 1;
  34. my @gateways =();
  35. #select undeleted mikrotik routers only
  36. if ($ARGV[0]) {
  37. my $router = get_record_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) and (user_acl=1 or dhcp=1) and deleted=0 and vendor_id=9 and id='.$ARGV[0]);
  38. if ($router) { push(@gateways,$router); }
  39. } else {
  40. @gateways = get_records_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) and (user_acl=1 or dhcp=1) and deleted=0 and vendor_id=9');
  41. }
  42. my $dhcp_networks = new Net::Patricia;
  43. my %dhcp_conf;
  44. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 ORDER BY ip_int_start');
  45. foreach my $subnet (@subnets) {
  46. next if (!$subnet->{gateway});
  47. my $subnet_name = $subnet->{subnet};
  48. $subnet_name=~s/\/\d+$//g;
  49. $dhcp_networks->add_string($subnet->{subnet},$subnet_name);
  50. $dhcp_conf{$subnet_name}->{first_pool_ip}=IpToStr($subnet->{dhcp_start});
  51. $dhcp_conf{$subnet_name}->{last_pool_ip}=IpToStr($subnet->{dhcp_stop});
  52. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  53. my $dhcp_info=GetDhcpRange($subnet->{subnet});
  54. $dhcp_conf{$subnet_name}->{first_ip} = $dhcp_info->{first_ip};
  55. $dhcp_conf{$subnet_name}->{last_ip} = $dhcp_info->{last_ip};
  56. $dhcp_conf{$subnet_name}->{first_ip_aton}=StrToIp($dhcp_info->{first_ip});
  57. $dhcp_conf{$subnet_name}->{last_ip_aton}=StrToIp($dhcp_info->{last_ip});
  58. }
  59. my $pm = Parallel::ForkManager->new($fork_count);
  60. #save changed records
  61. my @changes_found = get_records_sql($dbh,"SELECT id FROM User_auth WHERE changed=1");
  62. foreach my $gate (@gateways) {
  63. next if (!$gate);
  64. $pm->start and next;
  65. $dbh = init_db();
  66. my @cmd_list=();
  67. $gate = netdev_set_auth($gate);
  68. $gate->{login}.='+ct400w';
  69. my $t = netdev_login($gate);
  70. if (!$t) {
  71. log_error("Login to $gate->{device_name} [$gate->{ip}] failed! Skip gateway.");
  72. $dbh->disconnect();
  73. $pm->finish;
  74. next;
  75. }
  76. my $router_name=$gate->{device_name};
  77. my $router_ip=$gate->{ip};
  78. my $shaper_enabled = $gate->{queue_enabled};
  79. my $connected_users_only = $gate->{connected_user_only};
  80. my $connected_users = new Net::Patricia;
  81. my @changed_ref=();
  82. my @lan_int=();
  83. my @wan_int=();
  84. my @l3_int = get_records_sql($dbh,'SELECT * FROM device_l3_interfaces WHERE device_id='.$gate->{'id'});
  85. foreach my $l3 (@l3_int) {
  86. $l3->{'name'}=~s/\"//g;
  87. if ($l3->{'interface_type'} eq '0') { push(@lan_int,$l3->{'name'}); }
  88. if ($l3->{'interface_type'} eq '1') { push(@wan_int,$l3->{'name'}); }
  89. }
  90. 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'});
  91. if (@gw_subnets and scalar @gw_subnets) {
  92. foreach my $gw_subnet (@gw_subnets) {
  93. if ($gw_subnet and $gw_subnet->{'subnet'}) { $connected_users->add_string($gw_subnet->{'subnet'}); }
  94. }
  95. }
  96. foreach my $int (@lan_int) { #interface dhcp loop
  97. next if (!$int);
  98. $int=trim($int);
  99. #get ip addr at interface
  100. my @int_addr=netdev_cmd($gate,$t,$gate->{proto},'/ip address print terse without-paging where interface='.$int,1);
  101. log_debug("Get interfaces: ".Dumper(\@int_addr));
  102. my $found_subnet;
  103. foreach my $int_str(@int_addr) {
  104. $int_str=trim($int_str);
  105. next if (!$int_str);
  106. if ($int_str=~/\s+address=(\S*)\s+/i) {
  107. my $gate_interface=$1;
  108. if ($gate_interface) {
  109. my $gate_ip=$gate_interface;
  110. $gate_ip=~s/\/.*$//;
  111. #search for first match
  112. if (!$found_subnet) { $found_subnet=$dhcp_networks->match_string($gate_ip); }
  113. #all subnets match
  114. if ($connected_users_only) { $connected_users->add_string($gate_interface); }
  115. }
  116. }
  117. }
  118. if (!$found_subnet) { db_log_verbose($dbh,"DHCP subnet for interface $int not found! Skip interface."); next; }
  119. db_log_verbose($dbh,"Analyze interface $int. Found: ".Dumper($dhcp_conf{$found_subnet}));
  120. #dhcp config
  121. if ($gate->{dhcp}) {
  122. #fetch current dhcp records
  123. my @ret_static_leases=netdev_cmd($gate,$t,$gate->{proto},'/ip dhcp-server lease print terse without-paging where server=dhcp-'.$int,1);
  124. log_debug("Get dhcp leases:".Dumper(\@ret_static_leases));
  125. my @current_static_leases=();
  126. foreach my $str (@ret_static_leases) {
  127. next if (!$str);
  128. $str=trim($str);
  129. if ($str=~/^\d/) {
  130. log_debug("Found current static lease record: ".$str);
  131. push(@current_static_leases,$str);
  132. }
  133. }
  134. #select users for this interface
  135. my @auth_records=get_records_sql($dbh,"SELECT * from User_auth WHERE dhcp=1 and `ip_int`>=".$dhcp_conf{$found_subnet}->{first_ip_aton}." and `ip_int`<=".$dhcp_conf{$found_subnet}->{last_ip_aton}." and deleted=0 and ou_id !=".$default_user_ou_id." and ou_id !=".$default_hotspot_ou_id." ORDER BY ip_int");
  136. my %leases;
  137. foreach my $lease (@auth_records) {
  138. next if (!$lease);
  139. next if (!$lease->{mac});
  140. next if (!$lease->{ip});
  141. next if ($lease->{ip} eq $dhcp_conf{$found_subnet}->{relay_ip});
  142. $leases{$lease->{ip}}{ip}=$lease->{ip};
  143. $leases{$lease->{ip}}{comment}=$lease->{id};
  144. $leases{$lease->{ip}}{id}=$lease->{id};
  145. $leases{$lease->{ip}}{dns_name}=$lease->{dns_name};
  146. if ($lease->{comments}) { $leases{$lease->{ip}}{comment}=translit($lease->{comments}); }
  147. $leases{$lease->{ip}}{mac}=uc(mac_splitted($lease->{mac}));
  148. if ($lease->{dhcp_acl}) {
  149. $leases{$lease->{ip}}{acl}=trim($lease->{dhcp_acl});
  150. $leases{$lease->{ip}}{acl}=~s/;/,/g;
  151. }
  152. $leases{$lease->{ip}}{acl}='' if (!$leases{$lease->{ip}}{acl});
  153. }
  154. my %active_leases;
  155. foreach my $lease (@current_static_leases) {
  156. my @words = split(/\s+/,$lease);
  157. my %tmp_lease;
  158. if ($lease=~/^(\d*)\s+/) { $tmp_lease{id}=$1; };
  159. next if (!defined($tmp_lease{id}));
  160. foreach my $option (@words) {
  161. next if (!$option);
  162. $option=trim($option);
  163. next if (!$option);
  164. my @tmp = split(/\=/,$option);
  165. my $token = trim($tmp[0]);
  166. my $value = trim($tmp[1]);
  167. next if (!$token);
  168. next if (!$value);
  169. $value=~s/\"//g;
  170. if ($token=~/^address$/i) { $tmp_lease{ip}=GetIP($value); }
  171. if ($token=~/^mac-address$/i) { $tmp_lease{mac}=uc(mac_splitted($value)); }
  172. if ($token=~/^address-lists$/i) { $tmp_lease{acl}=$value; }
  173. }
  174. next if (!$tmp_lease{ip});
  175. next if (!$tmp_lease{mac});
  176. next if ($lease=~/^(\d*)\s+D\s+/);
  177. $active_leases{$tmp_lease{ip}}{ip}=$tmp_lease{ip};
  178. $active_leases{$tmp_lease{ip}}{mac}=$tmp_lease{mac};
  179. $active_leases{$tmp_lease{ip}}{id}=$tmp_lease{id};
  180. $active_leases{$tmp_lease{ip}}{acl}='';
  181. if ($tmp_lease{acl}) {
  182. $active_leases{$tmp_lease{ip}}{acl}=$tmp_lease{acl};
  183. }
  184. }
  185. log_debug("Active leases: ".Dumper(\%active_leases));
  186. #sync state
  187. foreach my $ip (keys %active_leases) {
  188. if (!exists $leases{$ip}) {
  189. db_log_verbose($dbh,"Address $ip not found in stat. Remove from router.");
  190. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  191. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  192. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  193. next;
  194. }
  195. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  196. db_log_verbose($dbh,"Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Remove lease from router.");
  197. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  198. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  199. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  200. next;
  201. }
  202. next if (!$leases{$ip}{acl} and !$active_leases{$ip}{acl});
  203. if ($leases{$ip}{acl}!~/$active_leases{$ip}{acl}/) {
  204. db_log_error($dbh,"Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Remove lease from router.");
  205. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  206. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  207. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  208. next;
  209. }
  210. }
  211. foreach my $ip (keys %leases) {
  212. my $acl='';
  213. if ($leases{$ip}{acl}) { $acl = 'address-lists='.$leases{$ip}{acl}; }
  214. my $comment = $leases{$ip}{comment};
  215. $comment =~s/\=//g;
  216. my $dns_name='';
  217. if ($leases{$ip}{dns_name}) { $dns_name = $leases{$ip}{dns_name}; }
  218. $dns_name =~s/\=//g;
  219. if ($dns_name) { $comment = 'comment="'.$dns_name." - ".$comment.'"'; } else { $comment = 'comment="'.$comment.'"'; }
  220. if (!exists $active_leases{$ip}) {
  221. db_log_verbose($dbh,"Address $ip not found in router. Create static lease record.");
  222. #remove static and dynamic records for mac
  223. 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};');
  224. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  225. #remove current ip binding
  226. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  227. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  228. #add new bind
  229. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' server=dhcp-'.$int.' '.$comment);
  230. #clear arp record
  231. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  232. next;
  233. }
  234. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  235. db_log_error($dbh,"Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Create static lease record.");
  236. #remove static and dynamic records for mac
  237. 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};');
  238. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  239. #remove current ip binding
  240. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  241. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  242. #add new bind
  243. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' server=dhcp-'.$int.' '.$comment);
  244. #clear arp record
  245. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  246. next;
  247. }
  248. next if (!$leases{$ip}{acl} and !$active_leases{$ip}{acl});
  249. if ($leases{$ip}{acl}!~/$active_leases{$ip}{acl}/) {
  250. db_log_error($dbh,"Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Create static lease record.");
  251. 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};');
  252. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  253. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' server=dhcp-'.$int.' '.$comment);
  254. #clear arp record
  255. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  256. next;
  257. }
  258. }
  259. }#end interface dhcp loop
  260. }#end dhcp config
  261. #access lists config
  262. if ($gate->{user_acl}) {
  263. db_log_verbose($dbh,"Sync user state at router $router_name [".$router_ip."] started.");
  264. #get userid list
  265. my $user_auth_sql="SELECT User_auth.ip, User_auth.filter_group_id, User_auth.queue_id, User_auth.id
  266. FROM User_auth, User_list
  267. WHERE User_auth.user_id = User_list.id
  268. AND User_auth.deleted =0
  269. AND User_auth.enabled =1
  270. AND User_auth.blocked =0
  271. AND User_list.blocked =0
  272. AND User_list.enabled =1
  273. AND User_auth.ou_id <> $default_hotspot_ou_id
  274. ORDER BY ip_int";
  275. my @authlist_ref = get_records_sql($dbh,$user_auth_sql);
  276. my %users;
  277. my %lists;
  278. my %found_users;
  279. foreach my $row (@authlist_ref) {
  280. if ($connected_users_only) { next if (!$connected_users->match_string($row->{ip})); }
  281. #skip not office ip's
  282. next if (!$office_networks->match_string($row->{ip}));
  283. $found_users{$row->{'id'}}=$row->{ip};
  284. #filter group acl's
  285. $users{'group_'.$row->{filter_group_id}}->{$row->{ip}}=1;
  286. $users{'group_all'}->{$row->{ip}}=1;
  287. $lists{'group_'.$row->{filter_group_id}}=1;
  288. #queue acl's
  289. if ($row->{queue_id}) { $users{'queue_'.$row->{queue_id}}->{$row->{ip}}=1; }
  290. }
  291. log_debug("Users status:".Dumper(\%users));
  292. #full list
  293. $lists{'group_all'}=1;
  294. #get queue list
  295. my @queuelist_ref = get_records_sql($dbh,"SELECT * FROM Queue_list");
  296. my %queues;
  297. foreach my $row (@queuelist_ref) {
  298. $lists{'queue_'.$row->{id}}=1;
  299. next if ((!$row->{Download}) and !($row->{Upload}));
  300. $queues{'queue_'.$row->{id}}{id}=$row->{id};
  301. $queues{'queue_'.$row->{id}}{down}=$row->{Download};
  302. $queues{'queue_'.$row->{id}}{up}=$row->{Upload};
  303. }
  304. log_debug("Queues status:".Dumper(\%queues));
  305. my @filterlist_ref = get_records_sql($dbh,"SELECT * FROM Filter_list where type=0");
  306. my %filters;
  307. my %dyn_filters;
  308. my $max_filter_rec = get_record_sql($dbh,"SELECT MAX(id) FROM Filter_list");
  309. my $max_filter_id = $max_filter_rec->{id};
  310. my $dyn_filters_base = $max_filter_id+1000;
  311. my $dyn_filters_index = $dyn_filters_base;
  312. foreach my $row (@filterlist_ref) {
  313. #if dst - ip address
  314. if (is_ip($row->{dst})) {
  315. $filters{$row->{id}}->{id}=$row->{id};
  316. $filters{$row->{id}}->{proto}=$row->{proto};
  317. $filters{$row->{id}}->{dst}=$row->{dst};
  318. $filters{$row->{id}}->{dstport}=$row->{dstport};
  319. $filters{$row->{id}}->{srcport}=$row->{srcport};
  320. #set false for dns dst flag
  321. $filters{$row->{id}}->{dns_dst}=0;
  322. } else {
  323. #if dst not ip - check dns record
  324. my @dns_record=ResolveNames($row->{dst},undef);
  325. my $resolved_ips = (scalar @dns_record>0);
  326. next if (!$resolved_ips);
  327. foreach my $resolved_ip (sort @dns_record) {
  328. next if (!$resolved_ip);
  329. #enable dns dst filters
  330. $filters{$row->{id}}->{dns_dst}=1;
  331. #add dynamic dns filter
  332. $filters{$dyn_filters_index}->{id}=$row->{id};
  333. $filters{$dyn_filters_index}->{proto}=$row->{proto};
  334. $filters{$dyn_filters_index}->{dst}=$resolved_ip;
  335. $filters{$dyn_filters_index}->{dstport}=$row->{dstport};
  336. $filters{$dyn_filters_index}->{srcport}=$row->{srcport};
  337. $filters{$dyn_filters_index}->{dns_dst}=0;
  338. #save new filter dns id for original filter id
  339. push(@{$dyn_filters{$row->{id}}},$dyn_filters_index);
  340. $dyn_filters_index++;
  341. }
  342. }
  343. }
  344. log_debug("Filters status:". Dumper(\%filters));
  345. log_debug("DNS-filters status:". Dumper(\%dyn_filters));
  346. #clean unused filter records
  347. do_sql($dbh,"DELETE FROM Group_filters WHERE group_id NOT IN (SELECT id FROM Group_list)");
  348. do_sql($dbh,"DELETE FROM Group_filters WHERE filter_id NOT IN (SELECT id FROM Filter_list)");
  349. my @grouplist_ref = get_records_sql($dbh,"SELECT `group_id`,`filter_id`,`order`,`action` FROM Group_filters ORDER BY Group_filters.group_id,Group_filters.order");
  350. my %group_filters;
  351. my $index = 0;
  352. my $cur_group;
  353. foreach my $row (@grouplist_ref) {
  354. if (!$cur_group) { $cur_group = $row->{group_id}; }
  355. if ($cur_group != $row->{group_id}) {
  356. $index = 0;
  357. $cur_group = $row->{group_id};
  358. }
  359. #if dst dns filter not found
  360. if (!$filters{$row->{filter_id}}->{dns_dst}) {
  361. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$row->{filter_id};
  362. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  363. $index++;
  364. } else {
  365. #if found dns dst filters - add
  366. if (exists $dyn_filters{$row->{filter_id}}) {
  367. my @dyn_ips = @{$dyn_filters{$row->{filter_id}}};
  368. if (scalar @dyn_ips >0) {
  369. for (my $i = 0; $i < scalar @dyn_ips; $i++) {
  370. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$dyn_ips[$i];
  371. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  372. $index++;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. log_debug("Group filters: ".Dumper(\%group_filters));
  379. my %cur_users;
  380. foreach my $group_name (keys %lists) {
  381. my @address_lists=netdev_cmd($gate,$t,$gate->{proto},'/ip firewall address-list print terse without-paging where list='.$group_name,1);
  382. log_debug("Get address lists:".Dumper(\@address_lists));
  383. foreach my $row (@address_lists) {
  384. $row=trim($row);
  385. next if (!$row);
  386. my @address=split(' ',$row);
  387. foreach my $row (@address) {
  388. if ($row=~/address\=(.*)/i) { $cur_users{$group_name}{$1}=1; }
  389. }
  390. }
  391. }
  392. #new-ips
  393. foreach my $group_name (keys %users) {
  394. foreach my $user_ip (keys %{$users{$group_name}}) {
  395. if (!exists($cur_users{$group_name}{$user_ip})) {
  396. db_log_verbose($dbh,"Add user with ip: $user_ip to access-list $group_name");
  397. push(@cmd_list,"/ip firewall address-list add address=".$user_ip." list=".$group_name);
  398. }
  399. }
  400. }
  401. #old-ips
  402. foreach my $group_name (keys %cur_users) {
  403. foreach my $user_ip (keys %{$cur_users{$group_name}}) {
  404. if (!exists($users{$group_name}{$user_ip})) {
  405. db_log_verbose($dbh,"Remove user with ip: $user_ip from access-list $group_name");
  406. 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};");
  407. }
  408. }
  409. }
  410. timestamp;
  411. #sync firewall rules
  412. #sync group chains
  413. my @chain_list=netdev_cmd($gate,$t,$gate->{proto},'/ip firewall filter print terse without-paging where chain=Users and action=jump',1);
  414. log_debug("Get firewall chains:".Dumper(\@chain_list));
  415. my %cur_chain;
  416. foreach my $jump_list (@chain_list) {
  417. next if (!$jump_list);
  418. $jump_list=trim($jump_list);
  419. if ($jump_list=~/jump-target=(\S*)\s+/i) {
  420. if ($1) { $cur_chain{$1}++; }
  421. }
  422. }
  423. #old chains
  424. foreach my $group_name (keys %cur_chain) {
  425. if (!exists($group_filters{$group_name})) {
  426. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=Users and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  427. } else {
  428. if ($cur_chain{$group_name} != 2) {
  429. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=Users and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  430. push (@cmd_list,"/ip firewall filter add chain=Users action=jump jump-target=".$group_name." src-address-list=".$group_name);
  431. push (@cmd_list,"/ip firewall filter add chain=Users action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  432. }
  433. }
  434. }
  435. #new chains
  436. foreach my $group_name (keys %group_filters) {
  437. if (!exists($cur_chain{$group_name})) {
  438. push (@cmd_list,"/ip firewall filter add chain=Users action=jump jump-target=".$group_name." src-address-list=".$group_name);
  439. push (@cmd_list,"/ip firewall filter add chain=Users action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  440. }
  441. }
  442. my %chain_rules;
  443. foreach my $group_name (sort keys %group_filters) {
  444. next if (!$group_name);
  445. next if (!exists($group_filters{$group_name}));
  446. my %group_filter = %{$group_filters{$group_name}};
  447. foreach my $filter_index (sort keys %group_filter) {
  448. my $filter = $group_filter{$filter_index};
  449. my $filter_id=$filter->{filter_id};
  450. next if (!$filters{$filter_id});
  451. next if ($filters{$filter_id}->{dns_dst});
  452. my $src_rule='chain='.$group_name;
  453. my $dst_rule='chain='.$group_name;
  454. if ($filter->{action}) {
  455. $src_rule=$src_rule." action=accept";
  456. $dst_rule=$dst_rule." action=accept";
  457. } else {
  458. $src_rule=$src_rule." action=reject";
  459. $dst_rule=$dst_rule." action=reject";
  460. }
  461. if ($filters{$filter_id}->{proto} and ($filters{$filter_id}->{proto}!~/all/i)) {
  462. $src_rule=$src_rule." protocol=".$filters{$filter_id}->{proto};
  463. $dst_rule=$dst_rule." protocol=".$filters{$filter_id}->{proto};
  464. }
  465. if ($filters{$filter_id}->{dst} and $filters{$filter_id}->{dst} ne '0/0') {
  466. $src_rule=$src_rule." src-address=".trim($filters{$filter_id}->{dst});
  467. $dst_rule=$dst_rule." dst-address=".trim($filters{$filter_id}->{dst});
  468. }
  469. #dstport and srcport
  470. if (!$filters{$filter_id}->{dstport}) { $filters{$filter_id}->{dstport}=0; }
  471. if (!$filters{$filter_id}->{srcport}) { $filters{$filter_id}->{srcport}=0; }
  472. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} ne '0') {
  473. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport})." src-port=".trim($filters{$filter_id}->{dstport});
  474. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport})." dst-port=".trim($filters{$filter_id}->{dstport});
  475. }
  476. if ($filters{$filter_id}->{dstport} eq '0' and $filters{$filter_id}->{srcport} ne '0') {
  477. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport});
  478. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport});
  479. }
  480. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} eq '0') {
  481. $src_rule=$src_rule." src-port=".trim($filters{$filter_id}->{dstport});
  482. $dst_rule=$dst_rule." dst-port=".trim($filters{$filter_id}->{dstport});
  483. }
  484. if ($src_rule ne $dst_rule) {
  485. push(@{$chain_rules{$group_name}},$src_rule);
  486. push(@{$chain_rules{$group_name}},$dst_rule);
  487. } else {
  488. push(@{$chain_rules{$group_name}},$src_rule);
  489. }
  490. }
  491. }
  492. #chain filters
  493. foreach my $group_name (sort keys %group_filters) {
  494. next if (!$group_name);
  495. my @get_filter=netdev_cmd($gate,$t,$gate->{proto},'/ip firewall filter print terse without-paging where chain='.$group_name,1);
  496. chomp(@get_filter);
  497. my @cur_filter=();
  498. my $chain_ok=1;
  499. foreach (my $f_index=0; $f_index<scalar(@get_filter); $f_index++) {
  500. my $filter_str=trim($get_filter[$f_index]);
  501. next if (!$filter_str);
  502. next if ($filter_str!~/^(\d){1,3}/);
  503. $filter_str=~s/[^[:ascii:]]//g;
  504. $filter_str=~s/^\d{1,3}\s+//;
  505. $filter_str=trim($filter_str);
  506. next if (!$filter_str);
  507. push(@cur_filter,$filter_str);
  508. }
  509. log_debug("Current filters:".Dumper(\@cur_filter));
  510. log_debug("New filters:".Dumper($chain_rules{$group_name}));
  511. #current state rules
  512. foreach (my $f_index=0; $f_index<scalar(@cur_filter); $f_index++) {
  513. my $filter_str=trim($cur_filter[$f_index]);
  514. if (!$chain_rules{$group_name}[$f_index] or $filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  515. print "Check chain $group_name error! $filter_str not found in new config. Recreate chain.\n";
  516. $chain_ok=0;
  517. last;
  518. }
  519. }
  520. #new rules
  521. if ($chain_ok and $chain_rules{$group_name} and scalar(@{$chain_rules{$group_name}})) {
  522. foreach (my $f_index=0; $f_index<scalar(@{$chain_rules{$group_name}}); $f_index++) {
  523. my $filter_str=trim($cur_filter[$f_index]);
  524. if (!$filter_str) {
  525. print "Check chain $group_name error! Not found: $chain_rules{$group_name}[$f_index]. Recreate chain.\n";
  526. $chain_ok=0;
  527. last;
  528. }
  529. $filter_str=~s/^\d//;
  530. $filter_str=trim($filter_str);
  531. if ($filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  532. print "Check chain $group_name error! Expected: $chain_rules{$group_name}[$f_index] Found: $filter_str. Recreate chain.\n";
  533. $chain_ok=0;
  534. last;
  535. }
  536. }
  537. }
  538. if (!$chain_ok) {
  539. push(@cmd_list,":foreach i in [/ip firewall filter find where chain=".$group_name." ] do={/ip firewall filter remove \$i};");
  540. foreach my $filter_str (@{$chain_rules{$group_name}}) {
  541. push(@cmd_list,'/ip firewall filter add '.$filter_str);
  542. }
  543. }
  544. }
  545. if ($shaper_enabled) {
  546. #shapers
  547. my %get_queue_type=();
  548. my %get_queue_tree=();
  549. my %get_filter_mangle=();
  550. my @tmp=netdev_cmd($gate,$t,$gate->{proto},'/queue type print terse without-paging where name~"pcq_(down|up)load"',1);
  551. log_debug("Get queues: ".Dumper(\@tmp));
  552. # 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
  553. #pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64
  554. foreach my $row (@tmp) {
  555. next if (!$row);
  556. $row = trim($row);
  557. next if ($row!~/^(\d){1,3}/);
  558. $row=~s/^\d{1,3}\s+//;
  559. next if (!$row);
  560. if ($row=~/name=pcq_(down|up)load_(\d){1,3}\s+/i) {
  561. next if (!$1);
  562. next if (!$2);
  563. my $direct = $1;
  564. my $index = $2;
  565. $get_queue_type{$index}{$direct}=$row;
  566. if ($row=~/pcq-rate=(\S*)\s+\S/i) {
  567. my $rate = $1;
  568. if ($rate=~/k$/i) { $rate =~s/k$//i; }
  569. $get_queue_type{$index}{$direct."-rate"}=$rate;
  570. }
  571. if ($row=~/pcq-classifier=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-classifier"}=$1; }
  572. if ($row=~/pcq-src-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-src-address-mask"}=$1; }
  573. if ($row=~/pcq-dst-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-dst-address-mask"}=$1; }
  574. }
  575. }
  576. @tmp=();
  577. @tmp=netdev_cmd($gate,$t,$gate->{proto},'/queue tree print terse without-paging where parent~"(download|upload)_root"',1);
  578. log_debug("Get root queues: ".Dumper(\@tmp));
  579. #print Dumper(\@tmp);
  580. # 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
  581. # 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
  582. foreach my $row (@tmp) {
  583. next if (!$row);
  584. $row = trim($row);
  585. next if ($row!~/^(\d)/);
  586. $row=~s/^(\d*)\s+//;
  587. next if (!$row);
  588. if ($row=~/queue=pcq_(down|up)load_(\d){1,3}/i) {
  589. if ($row=~/name=queue_(\d){1,3}_(\S*)_out\s+/i) {
  590. next if (!$1);
  591. next if (!$2);
  592. my $index = $1;
  593. my $int_name = $2;
  594. $get_queue_tree{$index}{$int_name}{up}=$row;
  595. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-parent'}=$1; }
  596. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-mark'}=$1; }
  597. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-queue'}=$1; }
  598. }
  599. if ($row=~/name=queue_(\d){1,3}_(\S*)_in\s+/i) {
  600. next if (!$1);
  601. next if (!$2);
  602. my $index = $1;
  603. my $int_name = $2;
  604. $get_queue_tree{$index}{$int_name}{down}=$row;
  605. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-parent'}=$1; }
  606. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-mark'}=$1; }
  607. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-queue'}=$1; }
  608. }
  609. }
  610. }
  611. @tmp=();
  612. @tmp=netdev_cmd($gate,$t,$gate->{proto},'/ip firewall mangle print terse without-paging where action=mark-packet and new-packet-mark~"(upload|download)_[0-9]{1,3}"',1);
  613. log_debug("Get firewall mangle rules for queues:".Dumper(\@tmp));
  614. # 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=""
  615. # 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=""
  616. foreach my $row (@tmp) {
  617. next if (!$row);
  618. $row = trim($row);
  619. next if ($row!~/^(\d){1,3}/);
  620. $row=~s/^\d{1,3}\s+//;
  621. next if (!$row);
  622. if ($row=~/new-packet-mark=upload_(\d){1,3}_(\S*)\s+/i) {
  623. next if (!$1);
  624. next if (!$2);
  625. my $index = $1;
  626. my $int_name = $2;
  627. $get_filter_mangle{$index}{$int_name}{up}=$row;
  628. if ($row=~/src-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-list'}=$1; }
  629. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-dev'}=$1; }
  630. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-mark'}=$1; }
  631. }
  632. if ($row=~/new-packet-mark=download_(\d){1,3}_(\S*)\s+/i) {
  633. next if (!$1);
  634. next if (!$2);
  635. my $index = $1;
  636. my $int_name = $2;
  637. $get_filter_mangle{$index}{$int_name}{down}=$row;
  638. if ($row=~/dst-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-list'}=$1; }
  639. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-mark'}=$1; }
  640. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-dev'}=$1; }
  641. }
  642. }
  643. log_debug("Queues type status:".Dumper(\%get_queue_type));
  644. log_debug("Queues tree status:".Dumper(\%get_queue_tree));
  645. log_debug("Firewall mangle status:".Dumper(\%get_filter_mangle));
  646. my %queue_type;
  647. my %queue_tree;
  648. my %filter_mangle;
  649. #generate new config
  650. foreach my $queue_name (keys %queues) {
  651. my $q_id=$queues{$queue_name}{id};
  652. my $q_up=$queues{$queue_name}{up}+1;
  653. my $q_down=$queues{$queue_name}{down}+1;
  654. #queue_types
  655. $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";
  656. $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";
  657. my $queue_ok=1;
  658. if (!$get_queue_type{$q_id}{up}) { $queue_ok=0; }
  659. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'up-rate'})>10) { $queue_ok=0; }
  660. if ($queue_ok and $get_queue_type{$q_id}{'up-classifier'}!~/src-address/i) { $queue_ok=0; }
  661. if (!$queue_ok) {
  662. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_upload_'.$q_id.'" ] do={/queue type remove $i};');
  663. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{up});
  664. }
  665. $queue_ok=1;
  666. if (!$get_queue_type{$q_id}{down}) { $queue_ok=0; }
  667. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'down-rate'})>10) { $queue_ok=0; }
  668. if ($queue_ok and $get_queue_type{$q_id}{'down-classifier'}!~/dst-address/i) { $queue_ok=0; }
  669. if (!$queue_ok) {
  670. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_download_'.$q_id.'" ] do={/queue type remove $i};');
  671. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{down});
  672. }
  673. #upload queue
  674. foreach my $int (@wan_int) {
  675. $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";
  676. $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=\"\"";
  677. $queue_ok=1;
  678. if (!$get_queue_tree{$q_id}{$int}{up}) { $queue_ok=0; }
  679. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-parent'} ne "upload_root_".$int)) { $queue_ok=0;}
  680. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  681. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-queue'} ne "pcq_upload_".$q_id)) { $queue_ok=0; }
  682. if (!$queue_ok) {
  683. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_out".'" ] do={/queue tree remove $i};');
  684. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{up});
  685. }
  686. $queue_ok=1;
  687. if (!$get_filter_mangle{$q_id}{$int}{up}) { $queue_ok=0; }
  688. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  689. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  690. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-dev'} ne $int)) { $queue_ok=0; }
  691. if (!$queue_ok) {
  692. 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};');
  693. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{up});
  694. }
  695. }
  696. #download
  697. foreach my $int (@lan_int) {
  698. next if (!$int);
  699. $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";
  700. $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=\"\"";
  701. $queue_ok=1;
  702. if (!$get_queue_tree{$q_id}{$int}{down}) { $queue_ok=0; }
  703. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-parent'} ne "download_root_".$int)) { $queue_ok=0; }
  704. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  705. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-queue'} ne "pcq_download_".$q_id)) { $queue_ok=0; }
  706. if (!$queue_ok) {
  707. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_in".'" ] do={/queue tree remove $i};');
  708. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{down});
  709. }
  710. $queue_ok=1;
  711. if (!$get_filter_mangle{$q_id}{$int}{down}) { $queue_ok=0; }
  712. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  713. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  714. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-dev'} ne $int)) { $queue_ok=0; }
  715. if (!$queue_ok) {
  716. 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};');
  717. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{down});
  718. }
  719. }
  720. #end shaper
  721. }
  722. }
  723. }#end access lists config
  724. if (scalar(@cmd_list)) {
  725. log_debug("Apply:");
  726. if ($debug) { foreach my $cmd (@cmd_list) { log_debug("$cmd"); } }
  727. eval {
  728. netdev_cmd($gate,$t,$gate->{proto},\@cmd_list,1);
  729. };
  730. if ($@) {
  731. $all_ok = 0;
  732. log_debug("Error programming gateway! Err: ".$@);
  733. }
  734. }
  735. db_log_verbose($dbh,"Sync user state at router $router_name [".$router_ip."] stopped.");
  736. $dbh->disconnect();
  737. $pm->finish;
  738. }
  739. $pm->wait_all_children;
  740. #clear changed
  741. if ($all_ok) {
  742. foreach my $row (@changes_found) {
  743. do_sql($dbh,"UPDATE User_auth SET changed=0 WHERE id=".$row->{id});
  744. }
  745. }
  746. if (IsMyPID($SPID)) { Remove_PID($SPID); };
  747. do_exit 0;