1
0

print-dnsmasq.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use English;
  11. use base;
  12. use FindBin '$Bin';
  13. use lib "/opt/Eye/scripts";
  14. use strict;
  15. use DBI;
  16. use Time::Local;
  17. use Net::Patricia;
  18. use NetAddr::IP;
  19. use Data::Dumper;
  20. use eyelib::config;
  21. use eyelib::main;
  22. use eyelib::logconfig;
  23. use eyelib::database;
  24. use eyelib::common;
  25. use eyelib::net_utils;
  26. use File::Basename;
  27. use File::Path;
  28. use Fcntl qw(:flock);
  29. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  30. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  31. binmode(STDOUT,':utf8');
  32. setpriority(0,0,19);
  33. my $dhcp_networks = new Net::Patricia;
  34. my %dhcp_conf;
  35. my %static_hole;
  36. my %mac_subnets;
  37. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 and hotspot=0 ORDER BY ip_int_start');
  38. foreach my $subnet (@subnets) {
  39. next if (!$subnet->{gateway});
  40. $dhcp_networks->add_string($subnet->{subnet});
  41. my $subnet_name = $subnet->{subnet};
  42. $subnet_name=~s/\/\d+$//g;
  43. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  44. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  45. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  46. my $dhcp=GetDhcpRange($subnet->{subnet});
  47. if ($subnet->{static}) {
  48. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{mac}="01:02:03:04:05:06";
  49. $static_hole{$dhcp_conf{$subnet_name}->{last_ip}}->{skip}=0;
  50. print "dhcp-range=net-$subnet_name,$dhcp_conf{$subnet_name}->{last_ip},$dhcp_conf{$subnet_name}->{last_ip},$dhcp->{mask},$subnet->{dhcp_lease_time}m\n";
  51. } else {
  52. print "dhcp-range=net-$subnet_name,$dhcp_conf{$subnet_name}->{first_ip},$dhcp_conf{$subnet_name}->{last_ip},$dhcp->{mask},$subnet->{dhcp_lease_time}m\n";
  53. }
  54. print "dhcp-option=net:net-$subnet_name,option:router,$dhcp_conf{$subnet_name}->{relay_ip}\n";
  55. }
  56. #get userid list
  57. my $sSQL="SELECT id,ip,ip_int,mac,description,dns_name,dhcp_option_set,dhcp_acl,ou_id FROM user_auth where dhcp=1 and deleted=0 ORDER by ip_int";
  58. my @users = get_records_sql($dbh,$sSQL);
  59. foreach my $row (@users) {
  60. next if (!$row);
  61. next if (!$dhcp_networks->match_string($row->{ip}));
  62. next if (!$row->{mac});
  63. next if (!$row->{ip});
  64. next if (is_default_ou($dbh,$row->{ou_id}));
  65. if (exists $static_hole{$row->{ip}}) { $static_hole{$row->{ip}}{skip}=1; }
  66. my $subnet = $dhcp_networks->match_string($row->{ip});
  67. $mac_subnets{$subnet} ||= {
  68. name => $subnet,
  69. macs => {}
  70. };
  71. if (exists $mac_subnets{$subnet}{macs}{$row->{mac}}) {
  72. my $old_row = $mac_subnets{$subnet}{macs}{$row->{mac}};
  73. db_log_warning($dbh,"Mac $row->{mac} already exists in DHCP fo subnet $subnet! auth_id: $row->{id} and auth_id: $old_row->{id}");
  74. next;
  75. }
  76. $mac_subnets{$subnet}{macs}{$row->{mac}} = $row;
  77. print '#Comment:'.$row->{description}."\n" if ($row->{description});
  78. my $dns_name = '';
  79. if ($row->{dns_name}) {
  80. print '#DNS:'.$row->{dns_name}."\n";
  81. $dns_name = ','.$row->{dns_name};
  82. }
  83. my $dhcp_set = '';
  84. if ($row->{dhcp_option_set}) {
  85. $dhcp_set = ',set:'.$row->{dhcp_option_set};
  86. }
  87. print 'dhcp-host='.$row->{mac}.$dns_name.','.$row->{ip}.$dhcp_set."\n";
  88. }
  89. foreach my $ip (keys %static_hole) {
  90. if (!$static_hole{$ip}{skip}) {
  91. print '#BlackHole for static subnet\n';
  92. print 'dhcp-host='.$static_hole{$ip}->{mac}.', '.$ip."\n";
  93. }
  94. }
  95. # DNS
  96. print "#--- DNS ---#\n";
  97. #get userid list
  98. my $uSQL = "
  99. SELECT id, ou_id, ip, dns_name, dhcp_hostname, dns_ptr_only
  100. FROM user_auth
  101. WHERE deleted = 0
  102. AND ip IS NOT NULL
  103. AND (
  104. (dns_name IS NOT NULL AND dns_name != '' AND dns_name NOT LIKE '%.')
  105. OR
  106. (dhcp_hostname IS NOT NULL AND dhcp_hostname != '')
  107. )
  108. ORDER BY ip_int
  109. ";
  110. @users = get_records_sql($dbh, $uSQL);
  111. foreach my $row (@users) {
  112. next if (!$row);
  113. next if (is_default_ou($dbh,$row->{ou_id}));
  114. next if (!$office_networks->match_string($row->{ip}));
  115. my $dns_name = trim($row->{dns_name});
  116. if ($dns_name) {
  117. $dns_name =~s/_/-/g;
  118. # $dns_name =~s/[\.]/-/g;
  119. $dns_name =~s/ /-/g;
  120. $dns_name =~s/-$//g;
  121. $dns_name = trim($dns_name);
  122. if ($dns_name and $dns_name!~/\.$domain_name$/) { $dns_name = $dns_name .".".$domain_name; }
  123. } else { $dns_name=''; }
  124. next if (!$dns_name);
  125. #if (!$row->{dns_ptr_only} and ($dns_name or $row->{dhcp_hostname})) {
  126. if (!$row->{dns_ptr_only} and $dns_name) {
  127. print '#Comment:'.$row->{description}."\n" if ($row->{description});
  128. if ($dns_name) {
  129. print '#DNS A-record '.$dns_name."\n";
  130. print 'address=/'.$dns_name.'/'.$row->{ip}."\n";
  131. }
  132. # else {
  133. # if ($row->{dhcp_hostname} and $row->{dhcp_hostname}!~/UNDEFINED/i) {
  134. # $dns_name = $row->{dhcp_hostname};
  135. # $dns_name = $dns_name .".".$domain_name; }
  136. # $dns_name =~s/_/-/g;
  137. ## $dns_name =~s/[\.]/-/g;
  138. # $dns_name =~s/ /-/g;
  139. # $dns_name =~s/-$//g;
  140. # $dns_name = trim($dns_name);
  141. # if ($dns_name) {
  142. # print '#DNS-from-DHCP A-record '.$dns_name."\n";
  143. # print 'address=/'.$dns_name.'/'.$row->{ip}."\n";
  144. # }
  145. # }
  146. #aliases
  147. if ($dns_name) {
  148. my $aSQL = "SELECT * FROM user_auth_alias WHERE auth_id = ? AND alias IS NOT NULL AND alias != '' AND alias NOT LIKE '%.'";
  149. my @aliases = get_records_sql($dbh, $aSQL, $row->{id});
  150. print '#DNS aliases for '.$dns_name."\n" if (@aliases and scalar @aliases);
  151. foreach my $alias (@aliases) {
  152. my $dns_alias = trim($alias->{alias});
  153. # $dns_alias =~s/$domain_name//i;
  154. $dns_alias =~s/_/-/g;
  155. $dns_alias =~s/[\.]/-/g;
  156. $dns_alias =~s/ /-/g;
  157. $dns_alias =~s/-$//g;
  158. $dns_alias = trim($dns_alias);
  159. if ($dns_alias and $dns_alias !~ /\.\Q$domain_name\E$/i) { $dns_alias = $dns_alias .".".$domain_name; }
  160. print 'address=/'.$dns_alias.'/'.$row->{ip}."\n" if ($dns_alias);
  161. }
  162. }
  163. }
  164. my $ptr_record='';
  165. if ($dns_name and $row->{ip}=~/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/) {
  166. $ptr_record=$4.".".$3.".".$2.".".$1.".in-addr.arpa";
  167. print '#PTR for '.$dns_name."\n";
  168. print 'ptr-record='.$ptr_record.','.$dns_name."\n";
  169. }
  170. }
  171. exit 0;