sync_mikrotik.pl 34 KB

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