print-dhcpd2.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin/";
  7. use strict;
  8. use DBI;
  9. use Time::Local;
  10. use Net::Patricia;
  11. use NetAddr::IP;
  12. use Data::Dumper;
  13. use eyelib::config;
  14. use eyelib::main;
  15. use eyelib::mysql;
  16. use eyelib::net_utils;
  17. use File::Basename;
  18. use File::Path;
  19. use utf8;
  20. binmode(STDOUT,':utf8');
  21. setpriority(0,0,19);
  22. my %dhcp_conf;
  23. my $connected = new Net::Patricia;
  24. my $dhcp_networks = new Net::Patricia;
  25. my $int_addr=do_exec('/sbin/ip addr show | grep "scope global"');
  26. foreach my $address (split(/\n/,$int_addr)) {
  27. if ($address=~/inet\s+(.*)\s+brd/i) {
  28. if ($1) { $connected->add_string($1); }
  29. }
  30. }
  31. 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');
  32. foreach my $subnet (@subnets) {
  33. next if (!$subnet->{gateway});
  34. $dhcp_networks->add_string($subnet->{subnet});
  35. my $subnet_name = $subnet->{subnet};
  36. $subnet_name=~s/\/\d+$//g;
  37. $dhcp_conf{$subnet_name}->{first_ip}=IpToStr($subnet->{dhcp_start});
  38. $dhcp_conf{$subnet_name}->{last_ip}=IpToStr($subnet->{dhcp_stop});
  39. $dhcp_conf{$subnet_name}->{relay_ip}=IpToStr($subnet->{gateway});
  40. my $dhcp=GetDhcpRange($subnet->{subnet});
  41. $dhcp_conf{$subnet_name}->{mask}=$dhcp->{mask};
  42. $dhcp_conf{$subnet_name}->{masklen}=$dhcp->{masklen};
  43. $dhcp_conf{$subnet_name}->{gateway}=IpToStr($subnet->{gateway});
  44. $dhcp_conf{$subnet_name}->{network}=$dhcp->{network};
  45. $dhcp_conf{$subnet_name}->{dhcp_lease_time}=$subnet->{dhcp_lease_time}*60;
  46. $dhcp_conf{$subnet_name}->{deny_unknown_clients} = $subnet->{static};
  47. if ($connected->match_string(IpToStr($subnet->{gateway}))) { $dhcp_conf{$subnet_name}->{relay_ip}='direct'; }
  48. }
  49. foreach my $zone (keys %dhcp_conf) {
  50. $dhcp_conf{$zone}->{first_aton} = StrToIp($dhcp_conf{$zone}->{first_ip});
  51. $dhcp_conf{$zone}->{last_aton} = StrToIp($dhcp_conf{$zone}->{last_ip});
  52. for (my $i=$dhcp_conf{$zone}->{first_aton}; $i <= $dhcp_conf{$zone}->{last_aton}; $i++) {
  53. $dhcp_conf{$zone}->{pool}->{$i}=0;
  54. }
  55. }
  56. my $dir_name = "/etc/dhcp/eye.d";
  57. my $new_dir = $dir_name.".new";
  58. if (! -d "$dir_name" ) { mkpath($dir_name); }
  59. if (! -d "$new_dir" ) { mkpath($new_dir); }
  60. #get userid list
  61. my $sSQL="SELECT id,ip,ip_int,mac,comments,dns_name FROM User_auth where dhcp=1 and deleted=0 and ou_id !=".$default_user_ou_id." and ou_id !=".$default_hotspot_ou_id." ORDER by ip_int";
  62. my @users = get_records_sql($dbh,$sSQL);
  63. foreach my $row (@users) {
  64. next if (!$row);
  65. next if (!$row);
  66. next if (!$dhcp_networks->match_string($row->{ip}));
  67. next if (!$row->{mac});
  68. next if (!$row->{ip});
  69. next if ($hotspot_networks->match_string($row->{ip}));
  70. my $info = $office_networks->match_string($row->{ip});
  71. next if (!$info);
  72. my $zone_name = $info->{subnet};
  73. $zone_name=~s/(\/\d+)$//;
  74. push(@{$dhcp_conf{$zone_name}->{conf}},"# Data for $row->{id} $row->{dns_name} $row->{comments}");
  75. if ($row->{dns_name}) {
  76. push(@{$dhcp_conf{$zone_name}->{conf}},"host ".$row->{id}." { hardware ethernet ".$row->{mac}."; fixed-address ".$row->{ip}."; option host-name ".$row->{dns_name}."; }");
  77. } else {
  78. push(@{$dhcp_conf{$zone_name}->{conf}},"host ".$row->{id}." { hardware ethernet ".$row->{mac}."; fixed-address ".$row->{ip}."; }");
  79. }
  80. $dhcp_conf{$zone_name}->{pool}->{$row->{ip_int}} = 1;
  81. }
  82. foreach my $zone (keys %dhcp_conf) {
  83. my $start_pool = 0;
  84. for (my $i=$dhcp_conf{$zone}->{first_aton}; $i <= $dhcp_conf{$zone}->{last_aton}; $i++) {
  85. if (($dhcp_conf{$zone}->{pool}->{$i} or $i==$dhcp_conf{$zone}->{last_aton}) and $start_pool) {
  86. my $conf_str="range dynamic-bootp ".IpToStr($start_pool)." ".IpToStr($i-1).";";
  87. push(@{$dhcp_conf{$zone}->{conf}},$conf_str);
  88. $start_pool = 0;
  89. }
  90. if (!$dhcp_conf{$zone}->{pool}->{$i} and !$start_pool) {
  91. $start_pool = $i;
  92. }
  93. }
  94. }
  95. write_to_file($new_dir."/eye.conf","#dynamic generated file");
  96. write_to_file($new_dir."/eye.conf",'shared-network "company" {',1);
  97. foreach my $zone (keys %dhcp_conf) {
  98. my $full_zone_path=$new_dir."/".$zone.".conf";
  99. write_to_file($full_zone_path,$dhcp_conf{$zone}->{conf});
  100. write_to_file($new_dir."/eye.conf",'subnet '.$dhcp_conf{$zone}->{network}.' netmask '.$dhcp_conf{$zone}->{mask}.' {',1);
  101. write_to_file($new_dir."/eye.conf","\toption routers ".$dhcp_conf{$zone}->{gateway}.';',1);
  102. write_to_file($new_dir."/eye.conf","\tmax-lease-time ".$dhcp_conf{$zone}->{dhcp_lease_time}.';',1);
  103. write_to_file($new_dir."/eye.conf","\tdefault-lease-time ".$dhcp_conf{$zone}->{dhcp_lease_time}.';',1);
  104. write_to_file($new_dir."/eye.conf","\tauthoritative;",1);
  105. write_to_file($new_dir."/eye.conf","\tallow duplicates;",1);
  106. write_to_file($new_dir."/eye.conf","\t".'include "'.$full_zone_path.'";',1);
  107. if ($dhcp_conf{$zone}->{deny_unknown_clients}) { write_to_file($new_dir."/eye.conf","\tdeny unknown-clients;",1); }
  108. write_to_file($new_dir."/eye.conf","\t}",1);
  109. }
  110. write_to_file($new_dir."/eye.conf",'}',1);
  111. exit 0;