sync_mikrotik.pl 34 KB

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