1 <?php
4 /* All available record types
5 */
6 $RecordTypes['aRecord'] = "aRecord";
7 $RecordTypes['mDRecord'] = "mDRecord";
8 $RecordTypes['mXRecord'] = "mXRecord";
9 $RecordTypes['nSRecord'] = "nSRecord";
10 $RecordTypes['pTRRecord'] = "relativeDomainName";
11 $RecordTypes['hInfoRecord'] = "hInfoRecord";
12 $RecordTypes['mInfoRecord'] = "mInfoRecord";
13 $RecordTypes['cNAMERecord'] = "relativeDomainName";
14 $RecordTypes['tXTRecord'] = "tXTRecord";
15 $RecordTypes['aFSDBRecord'] = "aFSDBRecord";
16 $RecordTypes['SigRecord'] = "SigRecord";
17 $RecordTypes['KeyRecord'] = "KeyRecord";
18 $RecordTypes['aAAARecord'] = "aAAARecord";
19 $RecordTypes['LocRecord'] = "LocRecord";
20 $RecordTypes['nXTRecord'] = "nXTRecord";
21 $RecordTypes['sRVRecord'] = "sRVRecord";
22 $RecordTypes['nAPTRRecord'] = "nAPTRRecord";
23 $RecordTypes['kXRecord'] = "kXRecord";
24 $RecordTypes['certRecord'] = "certRecord";
25 $RecordTypes['a6Record'] = "a6Record";
26 $RecordTypes['dSRecord'] = "dSRecord";
27 $RecordTypes['sSHFPRecord'] = "sSHFPRecord";
28 $RecordTypes['rRSIGRecord'] = "rRSIGRecord";
29 $RecordTypes['nSECRecord'] = "nSECRecord";
32 /* Return all record types
33 */
34 function getDnsRecordTypes($ForZones = false)
35 {
36 global $RecordTypes;
37 if($ForZones){
38 $tmp = $RecordTypes;
39 unset($tmp['cNAMERecord']);
40 unset($tmp['pTRRecord']);
41 unset($tmp['tXTRecord']);
42 return($tmp);
43 }else{
44 return($RecordTypes);
45 }
46 }
49 /* This fucntion is used to flip the ip address, for example
50 12.3.45 -> 45.3.12
51 Because some entries (like zones) are store like that 45.3.12.in-addr.arpa
52 but we want to display 12.3.45.
53 */
54 function FlipIp($ip)
55 {
56 $tmp = array_reverse(split("\.",$ip));
57 $new = "";
58 foreach($tmp as $section){
59 $new .= $section.".";
60 }
61 return(preg_replace("/.$/","",$new));
62 }
65 /* This function returns the zones specified for given host
66 */
67 function getDNSZoneEntries($config,$HostDn,$silent = false)
68 {
69 global $RecordTypes;
71 $ldap = $config->get_ldap_link();
72 $ldap->cd($config->current['BASE']);
74 /* Not all records are allowed within a zone entry
75 */
76 $SkipRecords = array("tXTRecord","cNAMERecord","pTRRecord");
78 /* Special sOArecords
79 */
80 $sOAREcords = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
82 /* Create tempalte for all fetched zone Data
83 */
84 $ZoneBase = array();
85 $ZoneBase['exists'] = false;
86 $ZoneBase['RECORDS'] = array();
87 $ZoneBase['zoneName'] = array();
88 $ZoneBase['dNSClass'] = array();
90 foreach($sOAREcords as $attr){
91 $ZoneBase[$attr] = "";
92 }
94 $Zones = array();
96 /* Get & Parse all zone entries
97 */
98 $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$HostDn,array("*"));
99 $tmp_res = array();
100 while($attrs = $ldap->fetch()) {
101 $tmp_res[] = $attrs;
102 }
104 /* Parse fetched zones
105 */
106 foreach($tmp_res as $attrs){
108 $zoneName = $attrs['zoneName'][0];
109 $Zones[$zoneName] = $ZoneBase;
110 $Zones[$zoneName]['exists'] = true;
112 /* Set basic attributes
113 */
114 foreach(array("zoneName","dNSClass") as $attr){
115 if(isset($attrs[$attr][0])){
116 $Zones[$zoneName][$attr] = $attrs[$attr][0];
117 }
118 }
120 /* Set initial zone name, to be able to detect if this entry was renamed
121 */
122 $Zones[$zoneName]['InitialzoneName'] = $zoneName;
124 /* Generate SOA entry
125 */
126 if(isset($attrs['sOARecord'][0])){
127 $tmp = split("\ ",$attrs['sOARecord'][0]) ;
128 $tmp2 = array();
130 /* Assign soa vars */
131 foreach($sOAREcords as $key => $name){
132 if(isset($tmp[$key])){
133 $Zones[$zoneName][$name] = $tmp[$key];
134 }else{
135 $Zones[$zoneName][$name] = "";
136 }
137 }
138 } // ENDE SOA Record
140 /* Get record attributes
141 */
142 foreach($RecordTypes as $name => $value){
144 /* Skip some attributes
145 */
146 if(in_array($name,$SkipRecords)) continue;
148 /* If there is a record attribute
149 */
150 if(isset($attrs[$name])){
152 /* get all entries
153 */
154 for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
155 $Zones[$zoneName]['RECORDS'][] = array("type"=>$name,"value"=>$attrs[$value][$i]);
156 }
157 }
158 }
160 /* Get reverse record ..
161 */
162 $ldap->ls("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",$attrs['dn'],array("zoneName"));
164 if($ldap->count() == 0){
165 if(!$silent){
166 print_red(sprintf(_("Can't find reverse zone for dns zone '%s'. Aborting parsing this zone."),$zoneName));
167 }
168 unset($Zones[$zoneName]);
169 }elseif($ldap->count()>1){
170 if(!$silent){
171 print_red(sprintf(_("Found more than one reverse zone for dns zone '%s'. Aborting parsing this zone."),$zoneName));
172 }
173 unset($Zones[$zoneName]);
174 }else{
175 $tmp = $ldap->fetch();
176 $Zones[$zoneName]['ReverseZone'] = FlipIp(str_replace(".in-addr.arpa","",$tmp['zoneName'][0]));
177 $Zones[$zoneName]['InitialReverseZone'] = FlipIp(str_replace(".in-addr.arpa","",$tmp['zoneName'][0]));
178 }
179 }
180 return($Zones);
181 }
184 /* This function compares two dns zone objects and returns an
185 * array with following indexes
186 * - delete, for dns which must be deleted (only if dns zone is removed)
187 * - rename, if a dn must be renamed, for example, the zoneName has changed
188 * - add, if there is a new dns account created
189 */
190 function getDNSZoneEntriesDiff($config,$newZones,$HostDn)
191 {
192 $oldZones = getDNSZoneEntries($config,$HostDn,true);
194 $sOAattributes = array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl");
196 $move = array();
197 $add = array();
198 $del = array();
200 /* Generate a template for zones with default values
201 */
202 $zoneBase = array();
203 $zoneBase['objectClass'] = array("top","dNSZone");
204 $zoneBase['zoneName'] = "";
205 $zoneBase['relativeDomainName'] = "@";
206 $zoneBase['dNSClass'] = "IN";
207 $zoneBase['sOARecord'] = "";
209 /* Contains all renamed zoneNames
210 * For zone entry udpdates
211 */
212 $PrePareZoneEntries = array();
214 /* Walk through all zones and detect renamed/added/deleted zones ...
215 */
216 foreach($newZones as $name => $zone){
218 /* This zone was renamed
219 */
220 if((!empty($zone['InitialzoneName'])) && ($zone['InitialzoneName'] != $zone['zoneName'])){
222 /* Move old zone to new position
223 */
224 $oldDn = "zoneName=".$zone['InitialzoneName'].",".$HostDn;
225 $newDn = "zoneName=".$zone['zoneName'].",".$HostDn;
226 $PrePareZoneEntries[$zone['InitialzoneName']] = $zone['zoneName'];
227 $move [$oldDn] = $newDn;
228 }
230 /* Get old zone if available
231 */
232 $oldZone=array();
233 if(!empty($oldZones[$zone['InitialzoneName']])){
234 $oldZone = $oldZones[$zone['InitialzoneName']];
235 }
237 /* Create forward zone entry and put it in our add queue
238 */
239 $newDn = "zoneName=".$zone['zoneName'].",".$HostDn;
240 $obj = $zoneBase;
241 $obj['zoneName'] = $zone['zoneName'];
243 /* Create sOARecord & add it to the obj
244 */
245 $soa = "";
246 foreach($sOAattributes as $attr){
247 $soa.=" ".$zone[$attr];
248 }
249 $obj['sOARecord'] = trim($soa);
250 $obj['nSRecord'] = $zone['sOAprimary'];
252 /* If reverse zone was renamed, move entry
253 */
254 if(!empty($zone['InitialReverseZone'])){
255 if($zone['InitialReverseZone'] != $zone['ReverseZone']){
256 $base = "zoneName=".$zone['zoneName'].",".$HostDn;
257 $oldRDn = "zoneName=". FlipIp($zone['InitialReverseZone']).".in-addr.arpa,".$base;
258 $newRDn = "zoneName=". FlipIp($zone['ReverseZone']).".in-addr.arpa,".$base;
259 $PrePareZoneEntries[FlipIp($zone['InitialReverseZone']).".in-addr.arpa"] = FlipIp($zone['ReverseZone']).".in-addr.arpa";
260 $move [$oldRDn] = $newRDn;
261 }
262 }
264 /* Append record entries
265 * Set old value to array, to ensure that
266 * they will be deleted if necessary
267 */
268 if(isset($oldZone['RECORDS'])){
269 foreach($oldZone['RECORDS'] as $rec){
270 $obj[$rec['type']] = array();
271 }
272 }
274 /* Add new Records
275 */
276 foreach($zone['RECORDS'] as $rec){
277 if(!isset($obj[$rec['type']])||!is_array($obj[$rec['type']])){
278 $obj[$rec['type']] = array();
279 }
280 $obj[$rec['type']][] = $rec['value'];
281 }
283 /* Append udpated Zone Forward Entry to our add queue
284 */
285 $add[$newDn] = $obj;
287 /* Create Reverse Entry
288 * And append it to our add queue
289 */
290 $zone['ReverseZone'] = FlipIp($zone['ReverseZone']).".in-addr.arpa";
291 $base = "zoneName=".$zone['zoneName'].",".$HostDn;
292 $newRDn = "zoneName=".$zone['ReverseZone'].",".$base;
293 $rObj = $obj;
294 $rObj['zoneName']= $zone['ReverseZone'];
295 $add[$newRDn] = $rObj;
297 /* Remove currently managed zone from oldZones.
298 * this gives us the ability to detect removed zones
299 */
300 if(isset($oldZones[$zone['InitialzoneName']])){
301 unset($oldZones[$zone['InitialzoneName']]);
302 }
303 }
305 /* The rest of our oldZones must be deleted
306 * because they are no longer available in newZones anymore.
307 */
308 foreach($oldZones as $zone) {
309 $oldDn = "zoneName=".$zone['InitialzoneName'].",".$HostDn;
310 $del[$oldDn] = "";
311 }
313 /* Check for entries which must be updated
314 */
315 $zoneUpdates = array();
316 $udpate = array();
317 if(count($PrePareZoneEntries)){
318 $ldap = $config->get_ldap_link();
319 foreach($PrePareZoneEntries as $FromZoneName => $ToZoneName){
320 $ldap->cd($HostDn);
321 $ldap->search("(&(objectClass=dNSZone)(zoneName=".$FromZoneName.")(!(relativeDomainName=@)))",array("zoneName"));
322 while($attrs = $ldap->fetch()){
323 $zoneUpdates[$attrs['dn']] = array("zoneName"=>$ToZoneName);
324 }
325 }
326 }
328 $ret = array("del" => $del , "move" => $move , "add" => $add,"zoneUpdates"=>$zoneUpdates);
329 return($ret);
330 }
333 /* This function returns the dns-host eintries for given
334 * name.
335 */
336 function getDNSHostEntries($config,$name,$silent = false)
337 {
338 global $RecordTypes;
340 $types = array();
341 $ret = array();
342 $ret['RECORDS'] = array();
343 $ret['dNSClass'] = "IN";
344 $ret['zoneName'] = "";
345 $ret['dNSTTL'] = "7440";
346 $ret['exists'] = false;
348 $ldap = $config->get_ldap_link();
349 $ldap->cd($config->current['BASE']);
351 /* First check all zones for an entry with the given name.
352 * If the name occurs in more than one entry alert the user ...
353 */
354 $foundIn = array();
355 $zones = getAvailableZones($config);
357 $zonesArr = array();
358 foreach($zones as $zoneMix){
359 $zoneIndex = split("/",$zoneMix);
360 if(!array_key_exists($zoneIndex[0],$zonesArr)) {
361 $zonesArr[$zoneIndex[0]] = array();
362 }
363 array_push($zonesArr[$zoneIndex[0]],$zoneIndex[1]);
364 }
366 foreach($zonesArr as $nameServer => $nameServerArr){
367 $foundInTmp = array();
368 foreach($nameServerArr as $zoneArr => $zone){
369 $zoneMix = $nameServer."/".$zone;
370 $zoneDn = getDNSZoneDN($config,$zoneMix);
371 $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=".$name.")(!(relativeDomainName=@)))", $zoneDn,$attrs = array("*"));
372 while($attrs = $ldap->fetch()){
373 $foundInTmp [$zoneMix] = $attrs['dn'];
374 $foundIn [$zoneMix] = $attrs['dn'];
375 }
376 }
377 }
379 /* No zone found which contains an entry for us
380 */
381 if(count($foundIn) == 0){
382 return($ret);
383 }
385 /* Get host informations from zone
386 */
387 $id_tmp = key($foundIn);
388 $ldap->cd($foundIn[$id_tmp]);
389 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("*"));
390 while($attrs = $ldap->fetch()){
392 /* If relative domainname == cn
393 * Try to read dnsclass / TTl / zone
394 */
395 if($attrs['relativeDomainName'][0] == $name){
396 $ret['exists'] = true;
397 $ret['zoneName'] = $id_tmp;
398 foreach(array("dNSClass","dNSTTL") as $atr){
399 if(isset($attrs[$atr][0])){
400 $ret[$atr] = $attrs[$atr][0];
401 }
402 }
403 }
405 /* Create list with all used records */
406 foreach($RecordTypes as $name => $value){
408 /* If there is a record attribute */
409 if(isset($attrs[$name])){
411 /* get all entries */
412 for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
413 $types[] = array( "type" => $name,
414 "value" => $attrs[$value][$i]);
415 }
416 }
417 }
418 $ret['RECORDS'] = $types;
419 }
420 return($ret);
421 }
425 /* This function compares two dns settings and returns an
426 * array with following indexes
427 * - delete, for dns which must be deleted (only if dns account is removed)
428 * - rename, if a dn must be renamed, for example, the relativeDomainName has changed
429 * - add, if there is a new dns account created
430 */
431 function getDNSHostEntriesDiff($config,$oldName,$newEntry,$newName)
432 {
433 global $RecordTypes;
435 $oldEntry = getDNSHostEntries($config,$oldName);
437 $add = array();
438 $del = array();
439 $move = array();
441 /* Don't go further if there is nothing to do
442 * Is no account / was no account
443 */
444 if(($newEntry['exists'] == false )&& ($oldEntry['exists'] == false)){
445 return(array("move"=>$move,"add"=>$add,"del"=>$del));
446 }
448 $zones = getAvailableZones($config);
449 $specialAttributes = array("cNAMERecord","pTRRecord");
450 $newRecords = array(); // Used to remember which records are removed
451 $zoneNameMix = $newEntry['zoneName'];
452 $zoneDn = getDNSZoneDN($config,$zoneNameMix);
453 $tmp = array_flip($zones);
454 $zoneName = getNameFromMix($zoneNameMix);
456 /* If reverseZone can't be resolved ... this
457 * can't be a valid entry, so remove this account
458 */
459 if(isset($tmp[$zoneNameMix])){
460 $reverseNameMix = $tmp[$zoneNameMix];
461 $reverseDn = getDNSZoneDN($config,$reverseNameMix);
462 if(empty($reverseDn)){
463 $newEntry['exists'] = false;
464 }
465 }else{
466 $newEntry['exists'] = false;
467 }
469 /* If account was edited prepare some
470 * attributes & arrays ... if required add some
471 * dns to $move
472 */
473 if($oldEntry['exists']){
475 /* Check if the account was removed
476 */
477 if($newEntry['exists'] == false){
478 $dn = "relativeDomainName=".$oldName.",".getDNSZoneDN($config,$oldEntry['zoneName']);
479 $del[$dn] ="";
480 return(array("move"=>$move,"add"=>$add,"del"=>$del));
481 }
483 /* Check if zoneName has changed
484 */
485 if(count($newEntry['RECORDS'])){
486 if($oldEntry['zoneName'] != $newEntry['zoneName']){
487 $oldzoneDn = getDNSZoneDN($config,$oldEntry['zoneName']);
488 $dn = "relativeDomainName=".$oldName.",".$oldzoneDn;
489 $dn2= "relativeDomainName=".$oldName.",".$zoneDn;
490 $move[$dn]=$dn2;
491 }
493 /* Check if host name has changed
494 */
495 if($oldName != $newName){
496 $dn = "relativeDomainName=".$oldName.",".$zoneDn;
497 $dn2= "relativeDomainName=".$newName.",".$zoneDn;
498 $move[$dn]=$dn2;
499 $dn = "relativeDomainName=".$oldName.",".$dn2;
500 $dn2= "relativeDomainName=".$newName.",".$dn2;
501 $move[$dn]=$dn2;
502 }
503 }
505 /* Prepare record entries
506 * Fill old records with array();
507 * To ensure that they will be deleted if they stay unused
508 */
509 foreach($oldEntry['RECORDS'] as $id => $rec){
510 $newRecords[$rec['type']] = array();
511 }
512 }
514 /* There must be at least one record in our entry
515 */
516 if((!count($newEntry['RECORDS'])) || (!$newEntry['exists'])){
517 $dn = "relativeDomainName=".$newName.",".getDNSZoneDN($config,$oldEntry['zoneName']);
518 $del[$dn] ="";
519 $ret = array("move"=>$move,"add"=>$add,"del"=>$del);
520 return($ret);
521 }
523 /* Prepare temp obj
524 */
525 $baseObj = array();
526 $baseObj['objectClass'] = array("top","dNSZone");
527 $baseObj['dNSTTL'] = $newEntry['dNSTTL'];
528 $baseObj['dNSClass'] = $newEntry['dNSClass'];
529 $baseObj['zoneName'] = $zoneName;
530 $baseObj['relativeDomainName']= $newName;
532 /* Add Container Object to zone
533 * (this possibly already exists, check this before writing to ldap)
534 */
535 $baseDn = "relativeDomainName=".$newName.",".$zoneDn;
536 $add[$baseDn] = $baseObj;
538 /* Add base obejct which contains all std records
539 */
540 $stdDn = "relativeDomainName=".$newName.",".$baseDn;
541 $add[$stdDn] = $baseObj;
543 /* Set defaults. Normaly only contains old record names.
544 * The old names will be set to array, to ensure that they will be deleted.
545 * Or overwritten and filled with new values.
546 */
547 foreach($newRecords as $name => $def){
548 if(!in_array($name,$specialAttributes)){
549 $add[$stdDn][$name] = $def;
550 }
551 }
553 /* Delete all OLD special attributes.
554 */
555 foreach($oldEntry['RECORDS'] as $id => $rec){
556 if(in_array($rec['type'],$specialAttributes)){
557 $deldn= "relativeDomainName=".$rec['value'].",".$baseDn;
558 $del[$deldn] = "";
559 }
560 }
563 /* Create new record entries
564 */
565 foreach($newEntry['RECORDS'] as $id => $rec){
566 /* Create object which contains special records
567 * like pTRRecord or CNAMERecord
568 */
569 if($rec['type'] == "pTRRecord"){
570 $PTRdn= "relativeDomainName=".$rec['value'].",".$baseDn;
571 $ptrObj = $baseObj;
572 $reverseName = getNameFromMix($reverseNameMix);
573 $ptrObj['zoneName'] = $reverseName;
574 if(!preg_match("/\.$/",$newName)){
575 $ptrObj['pTRRecord'] = preg_replace("/\.\.$/",".",$newName.".".$zoneName.".");
576 }else{
577 $ptrObj['pTRRecord'] = preg_replace("/\.\.$/",".",$newName.".");
578 .}
579 $ptrObj['relativeDomainName'] = $rec['value'];
581 $add[$PTRdn] = $ptrObj;
582 }else
583 if($rec['type'] == "cNAMERecord"){
584 $PTRdn= "relativeDomainName=".$rec['value'].",".$baseDn;
585 $ptrObj = $baseObj;
586 $ptrObj['zoneName'] = $zoneName;
587 $ptrObj['cNAMERecord'] = $newName;
588 $ptrObj['relativeDomainName'] = $rec['value'];
590 $add[$PTRdn] = $ptrObj;
591 }else{
592 /* Append basic attributes
593 */
594 $add[$stdDn][$rec['type']][] = $rec['value'];
595 }
596 } // foreach record
598 $ret = array("move"=>$move,"add"=>$add,"del"=>$del);
599 return($ret);
600 }
602 function getNameFromMix($zoneMix){
603 $ret = "";
604 if(!strstr($zoneMix, '/')) return($ret);
605 $zoneIndex = split("/",$zoneMix);
606 return($zoneIndex[1]);
607 }
609 /* returns the dn for a specified zone
610 */
611 function getDNSZoneDN($config,$zoneNameMix)
612 {
613 $ret = "";
614 if(!strstr($zoneNameMix, '/')) {
615 print_red(sprintf(_("Undefined zone name '%s'. Zone name must look like this 'server/zone.com'."),$zoneNameMix));
616 return($ret);
617 }
619 $zoneNameIndex = split("/",$zoneNameMix);
620 $zoneName = $zoneNameIndex[1];
621 $nameServer = strtolower($zoneNameIndex[0]);
622 $ldap = $config->get_ldap_link();
624 /* search for the nameserver */
625 $ldap-> cd($config->current['BASE']);
626 $ldap->search("(&(objectClass=goServer)(cn=".$nameServer."))",array("cn"));
627 if($ldap->count()){
628 $attr = $ldap->fetch();
629 } else {
630 return($ret);
631 }
633 $ldap-> cd($attr['dn']);
634 $ldap->search("(&(objectClass=dNSZone)(sOARecord=*)(zoneName=".$zoneName."))",array("zoneName"));
635 if($ldap->count()){
636 $attr = $ldap->fetch();
637 return($attr['dn']);
638 }
640 return($ret);
641 }
644 /* returns all available zones
645 * array[reverseName] = zoneName;
646 */
647 function getAvailableZones($config)
648 {
649 $ret = array();
650 $ldap = $config->get_ldap_link();
651 $ldap->cd ($config->current['BASE']);
653 /* Search for zones ...
654 */
655 $ldap->search("(&(objectClass=dNSZone)(sOARecord=*))",array("zoneName"));
657 $ForwardZones = array();
658 $ReverseZones = array();
659 $zones = array();
661 while($at = $ldap->fetch()){
662 if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){
663 $ReverseZones[$at['dn']] = $at;
664 }else{
665 $ForwardZones[$at['dn']] = $at;
666 }
667 }
669 foreach($ForwardZones as $dn => $obj){
671 /* try to find reverse
672 */
673 foreach($ReverseZones as $Rdn => $Robj ){
674 if(preg_match("/".$dn."/",$Rdn)){
675 $zones[strtoupper($ldap->getCn($dn))."/".$Robj['zoneName'][0]] =
676 strtoupper($ldap->getCn($dn))."/".$obj['zoneName'][0];
677 }
678 }
679 }
680 return($zones);
681 }
683 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
684 ?>