source: trunk/Modules/NetworkConfigLinux/Generators/DNS.php

Last change on this file was 948, checked in by chronos, 21 months ago
  • Fixed: IP address database field size in News table for IPv6.
File size: 14.9 KB
Line 
1<?php
2
3/* This is script for configuration of BIND DNS server.
4 * Use "sudo aptitude install bind9" to install server under Ubuntu
5 */
6
7class ConfigDNS extends NetworkConfigItem
8{
9 function GenerateDNS(array $DNS): void
10 {
11 $Output = '$ORIGIN '.$DNS['Domain'].'.'."\n".
12 '$TTL '.$DNS['TTL']."\n".
13 $DNS['Domain'].".\tIN\tSOA\t".$DNS['NameServer'][0].".\troot.".$DNS['Domain'].". (\n".
14 "\t\t\t".$DNS['Serial']."\t; serial\n".
15 "\t\t\t".$DNS['Refresh']."\t; refresh\n".
16 "\t\t\t".$DNS['Retry']."\t; retry\n".
17 "\t\t\t".$DNS['Expire']."\t; expire\n".
18 "\t\t\t".$DNS['Minimum']."\t; minimum\n".
19 "\t\t\t)\n";
20 foreach ($DNS['NameServer'] as $NameServer)
21 {
22 $Output .= "\t\tIN\tNS\t".strtolower($NameServer).".\n";
23 }
24 $Output .= "\t\t\tTXT\t".'"'.$DNS['Description'].'"'."\n";
25
26 // Mail server records
27 $Priority = 10;
28 foreach ($DNS['MailServer'] as $MailServer)
29 {
30 $Output .="\t\t\tMX\t".$Priority." ".strtolower($MailServer).".\n";
31 $Priority += 10;
32 }
33 $Output .= "; SPF\n".
34 $DNS['Domain'].".\tIN\tTXT\t\"v=spf1 mx -all\"\n".
35 $DNS['Domain'].".\tIN\tSPF\t\"v=spf1 mx -all\"\n";
36 foreach ($DNS['MailServer'] as $MailServer)
37 {
38 $Output .= $MailServer.".\tIN\tTXT\t\"v=spf1 a -all\"\n".
39 $MailServer.".\tIN\tSPF\t\"v=spf1 a -all\"\n";
40 }
41
42 // Base server
43 $Output .= "@\tIN\tA\t".strtolower($DNS['BaseServer'])."\n";
44 $Output .= "@\tIN\tAAAA\t".strtolower($DNS['BaseServerIPv6'])."\n";
45
46 // IPv4 host list
47 foreach ($DNS['Host'] as $Host)
48 {
49 if (strlen($Host['Name']) < 8) $Host['Name'] .= "\t";
50 $Output .= strtolower($Host['Name'])."\tIN\tA\t".$Host['Address']."\n";
51 }
52
53 // IPv6 host list
54 foreach ($DNS['Host'] as $Host)
55 {
56 if (strlen($Host['Name']) < 8) $Host['Name'] .= "\t";
57 if (array_key_exists('IPv6', $Host) and ($Host['IPv6'] != ''))
58 $Output .= strtolower($Host['Name'])."\tIN\tAAAA\t".$Host['IPv6']."\n";
59 }
60
61 // Alias list
62 foreach ($DNS['Alias'] as $Alias)
63 {
64 if (strlen($Alias['Name']) < 8) $Alias['Name'] .= "\t";
65 $Output .= strtolower($Alias['Name'])."\tIN\tCNAME\t".strtolower($Alias['Target'])."\n";
66 }
67
68 $File = fopen($DNS['BaseDir'].$DNS['Domain'].'.zone', 'w');
69 fputs($File, $Output);
70 fclose($File);
71
72 // Generate reverse DNS records
73 foreach ($DNS['Network'] as $Network)
74 {
75 $Parts = explode('.', $Network);
76 $Output = // '$ORIGIN '.$Parts[2].'.'.$Parts[1].'.'.$Parts[0].'.in-addr.arpa'."\n".
77 '$TTL '.$DNS['TTL']."\n".
78 "@\tIN\tSOA\t".$DNS['ReverseNameServer'][0].".\troot.".$DNS['Domain'].". (\n".
79 "\t\t\t\t".$DNS['Serial']."\t; serial\n".
80 "\t\t\t\t".$DNS['Refresh']."\t; refresh\n".
81 "\t\t\t\t".$DNS['Retry']."\t; retry\n".
82 "\t\t\t\t".$DNS['Expire']."\t; expire\n".
83 "\t\t\t\t".$DNS['Minimum']."\t; minimum\n".
84 "\t\t\t\t)\n";
85 foreach ($DNS['ReverseNameServer'] as $NameServer)
86 {
87 if (substr($NameServer, -strlen($DNS['Domain'])) == $DNS['Domain'])
88 $Output .= "@\tIN\tNS\t".$NameServer.".\n";
89 else $Output .= "\tIN\tNS\t".$NameServer.".\n";
90 }
91 foreach ($DNS['Host'] as $Host)
92 if (substr($Host['Address'], 0, strlen($Network)) == $Network)
93 {
94 $AddressParts = explode('.', $Host['Address']);
95 $Host['Name'] = strtolower($Host['Name']);
96 $Output .= $AddressParts[3]."\tIN\tPTR\t".$Host['Name'].".".$DNS['Domain'].".\n";
97 }
98 $File = fopen($DNS['BaseDir'].$Parts[2].'.'.$Parts[1].'.'.$Parts[0].'.in-addr.arpa.zone', 'w+');
99 fputs($File, $Output);
100 fclose($File);
101 }
102
103 // Generate reverse DNS IPv6 records
104 foreach ($DNS['IPv6Network'] as $Network)
105 {
106 $Parts = explode('/', $Network);
107 $NetworkAddress = $Parts[0];
108 $Prefix = $Parts[1];
109 $Output = // '$ORIGIN '.$Parts[2].'.'.$Parts[1].'.'.$Parts[0].'.in-addr.arpa'."\n".
110 '$TTL '.$DNS['TTL']."\n".
111 "@\tIN\tSOA\t".$DNS['ReverseNameServer'][0].".\troot.".$DNS['Domain'].". (\n".
112 "\t\t\t\t".$DNS['Serial']."\t; serial\n".
113 "\t\t\t\t".$DNS['Refresh']."\t; refresh\n".
114 "\t\t\t\t".$DNS['Retry']."\t; retry\n".
115 "\t\t\t\t".$DNS['Expire']."\t; expire\n".
116 "\t\t\t\t".$DNS['Minimum']."\t; minimum\n".
117 "\t\t\t\t)\n";
118 foreach ($DNS['ReverseNameServer'] as $NameServer)
119 {
120 if (substr($NameServer, -strlen($DNS['Domain'])) == $DNS['Domain'])
121 $Output .= "@\tIN\tNS\t".$NameServer.".\n";
122 else $Output .= "\tIN\tNS\t".$NameServer.".\n";
123 }
124 foreach ($DNS['Host'] as $Host)
125 if (array_key_exists('IPv6', $Host) and ($Host['IPv6'] != ''))
126 {
127 $Addr = new NetworkAddressIPv6();
128 $Addr->AddressFromString($Host['IPv6']);
129 $Octets = $Addr->GetOctets();
130 $Octets = array_slice($Octets, 0, (128 - $Prefix) / 4);
131 $Octets = implode('.', $Octets);
132
133 $Host['Name'] = strtolower($Host['Name']);
134 $Output .= $Octets."\tIN\tPTR\t".$Host['Name'].".".$DNS['Domain'].".\n";
135 }
136
137 $NetAddr = new NetworkAddressIPv6();
138 $NetAddr->AddressFromString($NetworkAddress);
139 $Octets = array_reverse($NetAddr->GetOctets());
140 $Octets = array_reverse(array_slice($Octets, 0, $Prefix / 4));
141 $FileName = implode('.', $Octets).'.ip6.arpa.zone';
142 $File = fopen($DNS['BaseDir'].$FileName, 'w+');
143 fputs($File, $Output);
144 fclose($File);
145 }
146 }
147
148 function Run(): void
149 {
150 $BaseDomain = 'zdechov.net';
151 $Now = getdate();
152 $I = floor(($Now['hours'] * 60 * 60 + $Now['minutes'] * 60 + $Now['seconds']) / (24 * 60 * 60) * 100);
153 $Serial = date('Ymd', time()).sprintf('%02d', $I);
154 $MinimumTime = 7200;
155 $RetryTime = 7200;
156 $ExpireTime = 2419200;
157 $RefreshTime = 28800;
158 $MinimumTime = 10800;
159 $TTL = 86400;
160 if (isset($this->System->Config['DNS']['BaseDir']))
161 $BaseDir = $this->System->Config['DNS']['BaseDir'];
162 else $BaseDir = '/var/cache/bind';
163 if (!file_exists($BaseDir)) die('Base directory "'.$BaseDir.'" not exists.');
164 $MailServer = 'centrala';
165
166
167 $BaseDomain = 'zdechov.net';
168 $LocalDNS = array(
169 'Domain' => $BaseDomain,
170 'Serial' => $Serial,
171 'Refresh' => $RefreshTime,
172 'Expire' => $ExpireTime,
173 'Retry' => $RetryTime,
174 'Minimum' => $MinimumTime,
175 'TTL' => $TTL,
176 'Description' => 'ZdechovNET community network',
177 'BaseDir' => $BaseDir.'/internal/',
178 'MailServer' => array('mail.'.$BaseDomain),
179 'NameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
180 'BaseServer' => '10.145.64.23', // warp
181 'BaseServerIPv6' => '2a00:e580:244:1::7', // warp
182 'ReverseNameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
183 'Host' => array(),
184 'Alias' => array(),
185 'Network' => array('10.145.64', '10.145.65', '10.145.66', '10.145.67', '10.145.68',
186 '10.145.69', '10.145.70', '10.145.71', '77.92.221', '172.16.0', '172.16.1'),
187 'IPv6Network' => array('2a00:e580:244::/48'),
188 );
189
190 // Local records
191 $DbResult = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` '.
192 'FROM `NetworkInterface` '.
193 'JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
194 'WHERE (`NetworkInterface`.`LocalIP` <> "") AND (`NetworkDevice`.`Used` = 1)');
195 while ($Interface = $DbResult->fetch_assoc())
196 {
197 $Name = $Interface['DeviceName'];
198 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
199 $LocalDNS['Host'][] = array('Name' => $Name, 'Address' => $Interface['LocalIP'],
200 'IPv6' => $Interface['IPv6']);
201 }
202
203 // External IPv4 records in internal server
204 $DbResult = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` FROM `NetworkInterface` '.
205 'JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
206 'WHERE (`NetworkInterface`.`ExternalIP` <> "") AND (`NetworkDevice`.`Used` = 1)');
207 while ($Interface = $DbResult->fetch_assoc())
208 {
209 $Name = $Interface['DeviceName'];
210 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
211 $LocalDNS['Host'][] = array('Name' => $Name.'-ext', 'Address' => $Interface['ExternalIP']);
212 }
213
214 // CZFree records in internal server
215 /*
216 $DbResult = $Database->query('SELECT NetworkInterface.*, NetworkDevice.Name AS DeviceName FROM NetworkInterface '.
217 'JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device WHERE NetworkInterface.CZFreeIP <> ""');
218 while ($Interface = $DbResult->fetch_assoc())
219 {
220 $Name = $Interface['DeviceName'];
221 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
222 $LocalDNS['Host'][] = array('Name' => $Name.'-czfree', 'Address' => $Interface['CZFreeIP']);
223 }
224 */
225
226 // Domain aliases
227 $DbResult = $this->Database->select('NetworkDomainAlias', '*');
228 while ($Alias = $DbResult->fetch_assoc())
229 {
230 $LocalDNS['Alias'][] = array('Name' => $Alias['Name'], 'Target' => $Alias['Target']);
231 }
232
233
234 // === External network DNS server ===
235 $ExternalDNS = array(
236 'Domain' => $BaseDomain,
237 'Serial' => $Serial,
238 'Refresh' => $RefreshTime,
239 'Expire' => $ExpireTime,
240 'Retry' => $RetryTime,
241 'Minimum' => $MinimumTime,
242 'TTL' => $TTL,
243 'Description' => 'ZdechovNET community network',
244 'BaseDir' => $BaseDir.'/external/',
245 'MailServer' => array('mail.'.$BaseDomain),
246 'NameServer' => array('gw-hajda.inext.cz', 'ns2.afraid.org', 'ns5.he.net'),
247 'BaseServer' => '77.92.221.215', // warp
248 'BaseServerIPv6' => '2a00:e580:244:1::7', // warp
249 'ReverseNameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
250 'Host' => array(),
251 'Alias' => array(),
252 'Network' => array('81.2.194', '193.86.238', '212.111.4', '77.92.221'),
253 'IPv6Network' => array('2a00:e580:244::/48'),
254 );
255
256 $DbResult = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` FROM `NetworkInterface` '.
257 'JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` WHERE '.
258 '(`NetworkInterface`.`ExternalIP` != "") AND (`NetworkDevice`.`Used` = 1)');
259 while ($Interface = $DbResult->fetch_assoc())
260 {
261 $Name = $Interface['DeviceName'];
262 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
263 $ExternalDNS['Host'][] = array('Name' => $Name, 'Address' => $Interface['ExternalIP'],
264 'IPv6' => $Interface['IPv6']);
265 }
266
267 // Domain alias
268 $DbResult = $this->Database->query('SELECT `NetworkDomainAlias`.* FROM `NetworkDomainAlias`');
269 // JOIN `NetworkDevice` ON NetworkDomainAlias.Target LIKE NetworkDevice.Name AND NetworkInterface.ExternalIP != ""');
270 while ($Alias = $DbResult->fetch_assoc())
271 {
272 $ExternalDNS['Alias'][] = array('Name' => $Alias['Name'], 'Target' => $Alias['Target']);
273 }
274
275
276 // === CZFree network DNS server ===
277 $BaseDomain = 'zdechovnet.czf';
278 $CZFreeDNS = array(
279 'Domain' => $BaseDomain,
280 'Serial' => date('Ymds', time()),
281 'Refresh' => 28800,
282 'Expire' => 2419200,
283 'Retry' => 7200,
284 'Minimum' => 10800,
285 'TTL' => 86400,
286 'Description' => 'ZdechovNET community network',
287 'BaseDir' => $BaseDir.'/czfree/',
288 'MailServer' => array('mail.'.$BaseDomain),
289 'NameServer' => array('centrala.'.$BaseDomain, 'mozek'.$BaseDomain),
290 'BaseServer' => '10.145.64.23', // warp
291 'BaseServerIPv6' => '2a00:e580:244:1::7', // warp
292 'ReverseNameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
293 'Host' => array(),
294 'Alias' => array(),
295 'Network' => array('10.145.64', '10.145.65', '10.145.66', '10.145.67',
296 '10.145.68', '10.145.69', '10.145.70', '10.145.71'),
297 'IPv6Network' => array(),
298 );
299
300 // Hosts
301 $DbResult = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` FROM `NetworkInterface` '.
302 'JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
303 'WHERE (`NetworkInterface`.`LocalIP` != "") AND (`NetworkDevice`.`Used` = 1)');
304 while ($Interface = $DbResult->fetch_assoc())
305 {
306 $Name = $Interface['DeviceName'];
307 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
308 $CZFreeDNS['Host'][] = array('Name' => $Name, 'Address' => $Interface['LocalIP']);
309 }
310
311 // Domain alias
312 $DbResult = $this->Database->query('SELECT `NetworkDomainAlias`.* FROM `NetworkDomainAlias`');
313 // JOIN `hosts` ON NetworkDomainAlias.Target LIKE hosts.name AND hosts.czfree_ip != ""');
314 while ($Alias = $DbResult->fetch_assoc())
315 {
316 $CZFreeDNS['Alias'][] = array('Name' => $Alias['Name'], 'Target' => $Alias['Target']);
317 }
318
319 // === CZFree network local address ===
320 $BaseDomain = 'zdechovnet.czf';
321 $CZFreeLocalDNS = array(
322 'Domain' => $BaseDomain,
323 'Serial' => date('Ymds', time()),
324 'Refresh' => 28800,
325 'Expire' => 2419200,
326 'Retry' => 7200,
327 'Minimum' => 10800,
328 'TTL' => 86400,
329 'Description' => 'ZdechovNET community network',
330 'BaseDir' => $BaseDir.'/internal/',
331 'MailServer' => array('mail.'.$BaseDomain),
332 'NameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
333 'BaseServer' => '10.145.64.23', // warp
334 'BaseServerIPv6' => '2a00:e580:244:1::7', // warp
335 'ReverseNameServer' => array('centrala.'.$BaseDomain, 'mozek.'.$BaseDomain),
336 'Host' => array(),
337 'Alias' => array(),
338 'Network' => array('10.145.64', '10.145.65', '10.145.66', '10.145.67',
339 '10.145.68', '10.145.69', '10.145.70', '10.145.71'),
340 'IPv6Network' => array(),
341 );
342
343 // Hosts
344 $DbResult = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName` FROM `NetworkInterface` '.
345 'JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` WHERE `NetworkInterface`.`LocalIP` != ""');
346 while ($Interface = $DbResult->fetch_assoc())
347 {
348 $Name = $Interface['DeviceName'];
349 if ($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
350 // $CZFreeLocalDNS['Host'][] = array('Name' => $Name.'-czfree', 'Address' => $Interface['LocalIP']);
351 $CZFreeLocalDNS['Host'][] = array('Name' => $Name, 'Address' => $Interface['LocalIP']);
352 }
353
354 // Domain alias
355 $DbResult = $this->Database->query('SELECT `NetworkDomainAlias`.* FROM `NetworkDomainAlias`');
356 // JOIN `hosts` ON NetworkDomainAlias.Target LIKE hosts.name AND hosts.czfree_ip != ""');
357 while ($Alias = $DbResult->fetch_assoc())
358 {
359 $CZFreeLocalDNS['Alias'][] = array('Name' => $Alias['Name'], 'Target' => $Alias['Target']);
360 }
361
362 $this->GenerateDNS($CZFreeDNS);
363 $this->GenerateDNS($CZFreeLocalDNS);
364
365 $this->GenerateDNS($LocalDNS);
366 $this->GenerateDNS($ExternalDNS);
367 }
368}
Note: See TracBrowser for help on using the repository browser.