sync_iptables.pl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use Time::Local;
  9. use FileHandle;
  10. use Data::Dumper;
  11. use Rstat::config;
  12. use Rstat::main;
  13. use Rstat::cmd;
  14. use Net::Patricia;
  15. use Date::Parse;
  16. use Rstat::net_utils;
  17. use Rstat::mysql;
  18. use IPTables::libiptc;
  19. use DBI;
  20. use utf8;
  21. use open ":encoding(utf8)";
  22. #exit;
  23. $|=1;
  24. my %fields=('device_name'=>'1', 'wan_int'=>'1', 'lan_int'=>'1', 'queue_enabled'=>'1', 'connected_user_only'=>'1');
  25. my $gate = get_record($dbh,'devices',\%fields,"deleted=0 and internet_gateway=1 and vendor_id=19 and device_name='".$HOSTNAME."'");
  26. if (!$gate) { exit 0; }
  27. my $router_name=$gate->{device_name};
  28. my $wan_dev = $gate->{wan_int};
  29. my $lan_dev = $gate->{lan_int};
  30. my $shaper_enabled = $gate->{queue_enabled};
  31. my $connected_users_only = $gate->{connected_user_only};
  32. my $connected_users = new Net::Patricia;
  33. if ($connected_users_only) {
  34. #lan interfaces
  35. my @lan_int=split(/;/,$lan_dev);
  36. foreach my $int (@lan_int) {
  37. $int=trim($int);
  38. next if (!$int);
  39. #get ip addr at interface
  40. foreach my $int_str (@lan_int) {
  41. $int_str=trim($int_str);
  42. my $int_addr=do_exec('/sbin/ip addr show '.$int_str.' | grep "scope global"');
  43. foreach my $address (split(/\n/,$int_addr)) {
  44. if ($address=~/inet\s+(.*)\s+brd/i) {
  45. if ($1) { $connected_users->add_string($1); }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. db_log_verbose($dbh,"Sync user state at router $router_name started.");
  52. #get userid list
  53. my $user_auth_sql="SELECT User_auth.ip, User_auth.filter_group_id
  54. FROM User_auth, User_list
  55. WHERE User_auth.user_id = User_list.id
  56. AND User_auth.deleted =0
  57. AND User_auth.enabled =1
  58. AND User_auth.blocked =0
  59. AND User_list.blocked =0
  60. AND User_auth.user_id <> $hotspot_user_id
  61. ORDER BY ip_int";
  62. my %users;
  63. my @authlist_ref = get_custom_records($dbh,$user_auth_sql);
  64. #print Dumper(\@authlist_ref);
  65. foreach my $row (@authlist_ref) {
  66. if ($connected_users_only) {
  67. next if (!$connected_users->match_string($row->{ip}));
  68. }
  69. $users{'group_'.$row->{filter_group_id}}->{ips}{$row->{ip}}=1;
  70. }
  71. #get filters
  72. my @filter_list = get_custom_records($dbh,"SELECT id,name,proto,dst,dstport,action FROM Filter_list where type=0");
  73. my %filters;
  74. foreach my $row (@filter_list) {
  75. $filters{$row->{id}}->{id}=$row->{id};
  76. $filters{$row->{id}}->{proto}=$row->{proto};
  77. $filters{$row->{id}}->{dst}=$row->{dst};
  78. $filters{$row->{id}}->{port}=$row->{dstport};
  79. $filters{$row->{id}}->{action}=$row->{action};
  80. }
  81. #get groups
  82. my @group_list = get_custom_records($dbh,"SELECT group_id,filter_id,Group_filters.order FROM Group_filters ORDER BY Group_filters.group_id,Group_filters.order" );
  83. my %group_filters;
  84. my %lists;
  85. my $index=0;
  86. foreach my $row (@group_list) {
  87. $group_filters{'group_'.$row->{group_id}}->{$index}=$row->{filter_id};
  88. $lists{'group_'.$row->{group_id}}=1;
  89. $index++;
  90. }
  91. my %cur_users;
  92. my @new_iptables_users=();
  93. foreach my $group_name (keys %lists) {
  94. #new users chains
  95. push(@new_iptables_users,"-A USERS -m set --match-set $group_name src -j $group_name");
  96. push(@new_iptables_users,"-A USERS -m set --match-set $group_name dst -j $group_name");
  97. #current user chains members
  98. my $address_lists=do_exec('/sbin/ipset list '.$group_name.' 2>/dev/null');
  99. $cur_users{$group_name}{found}=0;
  100. foreach my $row (split(/\n/,$address_lists)) {
  101. $row=trim($row);
  102. next if (!$row);
  103. if ($row=~/^Error$/i) { $cur_users{$group_name}{found}=0; last; }
  104. next if ($row !~ /^[0-9]/);
  105. $cur_users{$group_name}{ips}{$row}=1;
  106. $cur_users{$group_name}{found}=1;
  107. }
  108. }
  109. #recreate ipsets if not found
  110. foreach my $group_name (keys %lists) {
  111. next if ($cur_users{$group_name}{found});
  112. do_exec("/sbin/ipset create $group_name hash:net family inet maxelem 2655360 2>/dev/null");
  113. }
  114. my @cmd_list=();
  115. #new-ips
  116. foreach my $group_name (keys %users) {
  117. next if (!$users{$group_name}{ips});
  118. foreach my $user_ip (keys %{$users{$group_name}{ips}}) {
  119. if (!exists($cur_users{$group_name}{ips}{$user_ip})) {
  120. db_log_verbose($dbh,"Add user with ip: $user_ip to access-list $group_name");
  121. do_exec("/sbin/ipset add $group_name $user_ip");
  122. }
  123. }
  124. }
  125. #old-ips
  126. foreach my $group_name (keys %cur_users) {
  127. next if (!$cur_users{$group_name}{ips});
  128. foreach my $user_ip (keys %{$cur_users{$group_name}{ips}}) {
  129. if (!exists($users{$group_name}{ips}{$user_ip})) {
  130. db_log_verbose($dbh,"Remove user with ip: $user_ip from access-list $group_name");
  131. do_exec("/sbin/ipset del $group_name $user_ip");
  132. }
  133. }
  134. }
  135. timestamp;
  136. #filters
  137. my %chain_rules;
  138. foreach my $group_name (keys %lists) {
  139. next if (!$group_name);
  140. next if (!exists($group_filters{$group_name}));
  141. push(@{$chain_rules{$group_name}},"-N $group_name");
  142. foreach my $filter_index (sort keys %{$group_filters{$group_name}}) {
  143. my $filter_id=$group_filters{$group_name}->{$filter_index};
  144. next if (!$filters{$filter_id});
  145. my $src_rule='-A '.$group_name;
  146. my $dst_rule='-A '.$group_name;
  147. if ($filters{$filter_id}->{proto} and ($filters{$filter_id}->{proto}!~/all/i)) {
  148. $src_rule=$src_rule." -p ".$filters{$filter_id}->{proto};
  149. $dst_rule=$dst_rule." -p ".$filters{$filter_id}->{proto};
  150. }
  151. if ($filters{$filter_id}->{dst} and $filters{$filter_id}->{dst} ne '0/0') {
  152. $src_rule=$src_rule." -s ".trim($filters{$filter_id}->{dst});
  153. $dst_rule=$dst_rule." -d ".trim($filters{$filter_id}->{dst});
  154. }
  155. if ($filters{$filter_id}->{port} and $filters{$filter_id}->{port} ne '0') {
  156. my $module=" -m ".$filters{$filter_id}->{proto};
  157. if ($filters{$filter_id}->{port}=~/\-/ or $filters{$filter_id}->{port}=~/\,/ or $filters{$filter_id}->{port}=~/\:/) {
  158. $module=" -m multiport";
  159. $filters{$filter_id}->{port}=~s/\-/:/g;
  160. }
  161. $src_rule=$src_rule.$module." --sport ".trim($filters{$filter_id}->{port});
  162. $dst_rule=$dst_rule.$module." --dport ".trim($filters{$filter_id}->{port});
  163. }
  164. if ($filters{$filter_id}->{action}) {
  165. $src_rule=$src_rule." -j ACCEPT";
  166. $dst_rule=$dst_rule." -j ACCEPT";
  167. } else {
  168. $src_rule=$src_rule." -j REJECT";
  169. $dst_rule=$dst_rule." -j REJECT";
  170. }
  171. if ($src_rule ne $dst_rule) {
  172. push(@{$chain_rules{$group_name}},$src_rule);
  173. push(@{$chain_rules{$group_name}},$dst_rule);
  174. } else {
  175. push(@{$chain_rules{$group_name}},$src_rule);
  176. }
  177. }
  178. }
  179. ######## get current iptables USERS chain state
  180. my $cur_iptables = do_exec("/sbin/iptables --list-rules USERS 2>/dev/null");
  181. my @cur_iptables_users = split(/\n/,$cur_iptables);
  182. my $users_chain_ok=(scalar @cur_iptables_users eq scalar @new_iptables_users);
  183. #if count records in chain ok - check stuff
  184. if ($users_chain_ok) {
  185. for (my $i = 0; $i <= $#cur_iptables_users; $i++) {
  186. if ($cur_iptables_users[$i]!~/$new_iptables_users[$i]/i) { $users_chain_ok=0; last; }
  187. }
  188. }
  189. #group rules
  190. my %cur_chain_rules;
  191. foreach my $group_name (keys %lists) {
  192. next if (!$group_name);
  193. my $tmp=do_exec("/sbin/iptables --list-rules $group_name 2>/dev/null");
  194. foreach my $rule (split(/\n/,$tmp)) {
  195. if ($rule=~/Error/i) {
  196. $lists{$group_name}=0;
  197. last;
  198. }
  199. push(@{$cur_chain_rules{$group_name}},$rule);
  200. }
  201. }
  202. #check filter group chain
  203. foreach my $group_name (keys %lists) {
  204. my @tmp = ();
  205. if ($chain_rules{$group_name}) { @tmp = @{$chain_rules{$group_name}}; }
  206. my @cur_tmp = ();
  207. if ($cur_chain_rules{$group_name}) { @cur_tmp=@{$cur_chain_rules{$group_name}}; }
  208. my $group_chain_ok=($#tmp eq $#cur_tmp);
  209. #if count records in chain ok - check stuff
  210. if ($group_chain_ok) {
  211. for (my $i = 0; $i <= $#tmp; $i++) {
  212. if ($tmp[$i]!~/$cur_tmp[$i]/i) { $group_chain_ok=0; last; }
  213. }
  214. }
  215. if (!$group_chain_ok) {
  216. if ($lists{$group_name}) {
  217. push(@cmd_list,"-D USERS -m set --match-set $group_name src -j $group_name");
  218. push(@cmd_list,"-D USERS -m set --match-set $group_name dst -j $group_name");
  219. push(@cmd_list,"-D $group_name");
  220. }
  221. push(@cmd_list,@{$chain_rules{$group_name}});
  222. if ($users_chain_ok) {
  223. push(@cmd_list,"-A USERS -m set --match-set $group_name src -j $group_name");
  224. push(@cmd_list,"-A USERS -m set --match-set $group_name dst -j $group_name");
  225. }
  226. }
  227. }
  228. #recreate users chain
  229. if (!$users_chain_ok) {
  230. for (my $i = 0; $i <= $#new_iptables_users; $i++) {
  231. push(@cmd_list,$new_iptables_users[$i]);
  232. }
  233. }
  234. my $table = IPTables::libiptc::init('filter');
  235. foreach my $row (@cmd_list) {
  236. print "$row\n" if ($debug);
  237. my @cmd_array = split(" ",$row);
  238. $table->iptables_do_command(\@cmd_array);
  239. }
  240. $table->commit();
  241. db_log_verbose($dbh,"Sync user state at router $router_name stopped.");
  242. $dbh->disconnect();
  243. exit;