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

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