Code

Removed Current main base settings
[gosa.git] / include / functions_dns.inc
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;
70  
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();
89    
90   foreach($sOAREcords as $attr){
91     $ZoneBase[$attr] = "";
92   }
93  
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 
139   
140     /* Get record attributes 
141     */
142     foreach($RecordTypes as $name => $value){
143   
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);
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)
192   $oldZones = getDNSZoneEntries($config,$HostDn,true);
193   
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();
213   
214   /* Walk through all zones and detect renamed/added/deleted zones ... 
215    */
216   foreach($newZones as $name => $zone){
217     
218     /* This zone was renamed 
219      */
220     if((!empty($zone['InitialzoneName'])) && ($zone['InitialzoneName'] != $zone['zoneName'])){
221       
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'];
242  
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);
251     /* If reverse zone was renamed, move entry 
252      */
253     if(!empty($zone['InitialReverseZone'])){
254       if($zone['InitialReverseZone'] != $zone['ReverseZone']){
255         $base = "zoneName=".$zone['zoneName'].",".$HostDn;
256         $oldRDn = "zoneName=". FlipIp($zone['InitialReverseZone']).".in-addr.arpa,".$base; 
257         $newRDn = "zoneName=". FlipIp($zone['ReverseZone']).".in-addr.arpa,".$base;
258         $PrePareZoneEntries[FlipIp($zone['InitialReverseZone']).".in-addr.arpa"] = FlipIp($zone['ReverseZone']).".in-addr.arpa";
259         $move [$oldRDn] = $newRDn;
260       }
261     }
263     /* Append record entries 
264      *  Set old value to array, to ensure that 
265      *  they will be deleted if necessary
266      */
267     if(isset($oldZone['RECORDS'])){
268       foreach($oldZone['RECORDS'] as $rec){
269         $obj[$rec['type']] = array();
270       }
271     }
273     /* Add new Records 
274      */
275     foreach($zone['RECORDS'] as $rec){
276       $obj[$rec['type']][] = $rec['value'];
277     }
279     /* Append udpated Zone Forward Entry to our add queue
280      */    
281     $add[$newDn] = $obj;   
283     /* Create Reverse Entry 
284      * And append it to our add queue
285      */
286     $zone['ReverseZone'] = FlipIp($zone['ReverseZone']).".in-addr.arpa";
287     $base = "zoneName=".$zone['zoneName'].",".$HostDn;
288     $newRDn = "zoneName=".$zone['ReverseZone'].",".$base;
289     $rObj = $obj;
290     $rObj['zoneName']= $zone['ReverseZone'];
291     $add[$newRDn] = $rObj;
292  
293     /* Remove currently managed zone from oldZones.
294      *  this gives us the ability to detect removed zones
295      */
296     if(isset($oldZones[$zone['InitialzoneName']])){
297       unset($oldZones[$zone['InitialzoneName']]); 
298     }
299   }
300  
301   /* The rest of our oldZones must be deleted
302    *  because they are no longer available in newZones anymore.
303    */
304   foreach($oldZones as $zone)  {
305     $oldDn = "zoneName=".$zone['InitialzoneName'].",".$HostDn;
306     $del[$oldDn] = "";
307   }
309   /* Check for entries which must be updated 
310    */
311   $zoneUpdates = array();
312   $udpate = array();
313   if(count($PrePareZoneEntries)){
314     $ldap = $config->get_ldap_link();
315     foreach($PrePareZoneEntries as $FromZoneName => $ToZoneName){
316       $ldap->cd($HostDn);
317       $ldap->search("(&(objectClass=dNSZone)(zoneName=".$FromZoneName.")(!(relativeDomainName=@)))",array("zoneName"));
318       while($attrs = $ldap->fetch()){
319         $zoneUpdates[$attrs['dn']] = array("zoneName"=>$ToZoneName);
320       }
321     }
322   }
324   $ret = array("del" => $del , "move" => $move , "add" => $add,"zoneUpdates"=>$zoneUpdates);
325   return($ret);
329 /* This function returns the dns-host eintries for given 
330  *  name.
331  */
332 function getDNSHostEntries($config,$name,$silent = false)
334   global $RecordTypes;
336   $types = array();
337   $ret = array();
338   $ret['RECORDS']   = array();
339   $ret['dNSClass']  = "IN";
340   $ret['zoneName']  = "";
341   $ret['dNSTTL']    = "7440";
342   $ret['exists']    = false;
343  
344   $ldap = $config->get_ldap_link();
345   $ldap->cd($config->current['BASE']);
346   
347   /* First check all zones for an entry with the given name.
348    * If the name occurs in more than one entry alert the user ...
349    */
350   $foundIn = array();
351   $zones = getAvailableZones($config);
352   foreach($zones as $zone){
353     $zoneDn = getDNSZoneDN($config,$zone);
354     $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=".$name.")(!(relativeDomainName=@)))", $zoneDn,$attrs = array("*"));
355     while($attrs = $ldap->fetch()){
356       $foundIn [] = $attrs['dn'];
357     }
358   }
360   /* Alert if there is more than one zone with an entry named like $name
361    */ 
362   if((count($foundIn) > 1) && (!$silent)) {
363     print_red(sprintf(_("Found more than one dns zone which contains an entry named '%s'. Aborting getting dns informations for this device."),$name));
364   }
366   /* No zone found which contains an entry for us
367    */
368   if(count($foundIn) == 0){
369     return($ret);
370   }
372   /* Get host informations from zone
373    */ 
374   $ldap->cd($foundIn[0]);
375   $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("*"));
376   while($attrs = $ldap->fetch()){
378     /* If relative domainname == cn
379      * Try to read dnsclass / TTl / zone
380      */
381     if($attrs['relativeDomainName'][0] == $name){
382       $ret['exists'] = true;
383       foreach(array("dNSClass","zoneName","dNSTTL") as $atr){
384         if(isset($attrs[$atr][0])){
385           $ret[$atr] = $attrs[$atr][0];
386         }
387       }
388     }
390     /* Create list with all used records */
391     foreach($RecordTypes as $name => $value){
393       /* If there is a record attribute  */
394       if(isset($attrs[$name])){
396         /* get all entries */
397         for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
398           $types[] = array( "type"    => $name,
399                             "value"   => $attrs[$value][$i]);
400         }
401       }
402     }
403     $ret['RECORDS'] = $types;
404   }
405   return($ret);
406 }  
410 /* This function compares two dns settings and returns an 
411  *  array with following indexes 
412  *   - delete, for dns which must be deleted (only if dns account is removed)
413  *   - rename, if a dn must be renamed, for example, the relativeDomainName has changed
414  *   - add,    if there is a new dns account created    
415  */
416 function getDNSHostEntriesDiff($config,$oldName,$newEntry,$newName)
418   global $RecordTypes;
420   $oldEntry = getDNSHostEntries($config,$oldName);
422   $add    = array();
423   $del    = array();
424   $move   = array();
426   $zones              = getAvailableZones($config);
427   $specialAttributes  = array("cNAMERecord","pTRRecord");
428   $newRecords         = array();  // Used to remember which records are removed 
429   $zoneName           = $newEntry['zoneName'];
430   $zoneDn             = getDNSZoneDN($config,$zoneName);
431   $tmp                = array_flip($zones);
432  
433   /* If reverseZone can't be resolved ... this 
434    *  can't be a valid entry, so remove this account
435    */ 
436   if(isset($tmp[$zoneName])){
437     $reverseName  = $tmp[$zoneName];
438     $reverseDn    = getDNSZoneDN($config,$reverseName);
439     if(empty($reverseDn)){
440       $newEntry['exists'] = false;
441     }
442   }else{
443     $newEntry['exists'] = false;
444   }
446   /* Don't go further if there is nothing to do
447    * Is no account / was no account
448    */
449   if(($newEntry['exists'] == false )&& ($oldEntry['exists'] == false)){
450     return(array("move"=>$move,"add"=>$add,"del"=>$del));
451   }
452   
453   /* If account was edited prepare some
454    *  attributes & arrays ... if required add some 
455    *  dns to $move 
456    */
457   if($oldEntry['exists']){
459     /* Check if the account was removed 
460      */
461     if($newEntry['exists'] == false){
462       $dn = "relativeDomainName=".$oldName.",".getDNSZoneDN($config,$oldEntry['zoneName']);
463       $del[$dn] ="";
464       return(array("move"=>$move,"add"=>$add,"del"=>$del));
465     }
467     /* Check if zoneName has changed 
468      */   
469     if(count($newEntry['RECORDS'])){
470       if($oldEntry['zoneName'] != $newEntry['zoneName']){
471         $oldzoneDn = getDNSZoneDN($config,$oldEntry['zoneName']);
472         $dn = "relativeDomainName=".$oldName.",".$oldzoneDn;
473         $dn2= "relativeDomainName=".$oldName.",".$zoneDn;
474         $move[$dn]=$dn2;
475       }
477       /* Check if host name has changed 
478        */ 
479       if($oldName != $newName){
480         $dn = "relativeDomainName=".$oldName.",".$zoneDn;
481         $dn2= "relativeDomainName=".$newName.",".$zoneDn;
482         $move[$dn]=$dn2;
483         $dn = "relativeDomainName=".$oldName.",".$dn2;
484         $dn2= "relativeDomainName=".$newName.",".$dn2;
485         $move[$dn]=$dn2;
486       }
487     }
489     /* Prepare record entries
490      *  Fill old records with array();
491      *  To ensure that they will be deleted if they stay unused
492      */
493     foreach($oldEntry['RECORDS'] as $id => $rec){
494       $newRecords[$rec['type']] = array();
495     }
496   }
498   /* There must be at least one record in our entry  
499    */
500   if((!count($newEntry['RECORDS'])) || (!$newEntry['exists'])){
501     $dn = "relativeDomainName=".$newName.",".getDNSZoneDN($config,$oldEntry['zoneName']);
502     $del[$dn] ="";
503     $ret = array("move"=>$move,"add"=>$add,"del"=>$del);
504     return($ret);
505   }
507   /* Prepare temp obj
508    */
509   $baseObj =  array();
510   $baseObj['objectClass']       = array("top","dNSZone");
511   $baseObj['dNSTTL']            = $newEntry['dNSTTL'];
512   $baseObj['dNSClass']          = $newEntry['dNSClass'];
513   $baseObj['zoneName']          = $zoneName; 
514   $baseObj['relativeDomainName']= $newName; 
515     
516   /* Add Container Object to zone
517    *  (this possibly already exists, check this before writing to ldap)
518    */
519   $baseDn =  "relativeDomainName=".$newName.",".$zoneDn;
520   $add[$baseDn] = $baseObj;
522   /* Add base obejct which contains all std records
523    */
524   $stdDn = "relativeDomainName=".$newName.",".$baseDn;
525   $add[$stdDn] = $baseObj;
527   /* Set defaults. Normaly only contains old record names.
528    *  The old names will be set to array, to ensure that they will be deleted.
529    *  Or overwritten and filled with new values.
530    */
531   foreach($newRecords as $name => $def){
532     if(!in_array($name,$specialAttributes)){
533       $add[$stdDn][$name] = $def;
534     }
535   }
537   /* Delete all OLD special attributes.
538    */
539   foreach($oldEntry['RECORDS'] as $id => $rec){
540     if(in_array($rec['type'],$specialAttributes)){
541       $deldn= "relativeDomainName=".$rec['value'].",".$baseDn;
542       $del[$deldn] = "";
543     }
544   }
547   /* Create new record entries 
548    */
549   foreach($newEntry['RECORDS'] as $id => $rec){
550     /* Create object which contains special records 
551      *  like pTRRecord or CNAMERecord
552      */
553     if($rec['type']  == "pTRRecord"){
554       $PTRdn= "relativeDomainName=".$rec['value'].",".$baseDn;
555       $ptrObj = $baseObj;
556       $ptrObj['zoneName']           = $reverseName;
557       $ptrObj['pTRRecord']          = $newName.".".$zoneName;
558       $ptrObj['relativeDomainName'] = $rec['value'];
559       
560       $add[$PTRdn] = $ptrObj;
561     }else  
562     if($rec['type']  == "cNAMERecord"){
563       $PTRdn= "relativeDomainName=".$rec['value'].",".$baseDn;
564       $ptrObj = $baseObj;
565       $ptrObj['zoneName']           = $reverseName;
566       $ptrObj['cNAMERecord']        = $newName.".".$zoneName;
567       $ptrObj['relativeDomainName'] = $rec['value'];
568       
569       $add[$PTRdn] = $ptrObj;
570     }else{
571       /* Append basic attributes
572        */
573       $add[$stdDn][$rec['type']][] = $rec['value'];  
574     }
575   } // foreach record 
577   $ret = array("move"=>$move,"add"=>$add,"del"=>$del);
578   return($ret);
579
582 /* returns the dn for a specified zone
583  */
584 function getDNSZoneDN($config,$zoneName){
585   $ret = "";
586   $ldap = $config->get_ldap_link();
587   $ldap-> cd($config->current['BASE']);
588   $ldap->search("(&(objectClass=dNSZone)(sOARecord=*)(zoneName=".$zoneName."))",array("zoneName"));
589   if($ldap->count()){
590     $attr = $ldap->fetch();
591     return($attr['dn']);
592   }
593   return($ret);
597 /* returns all available zones 
598  *  array[reverseName] = zoneName;
599  */
600 function getAvailableZones($config)
602   $ret = array();
603   $ldap = $config->get_ldap_link();
604   $ldap->cd ($config->current['BASE']);
606   /* Search for zones ...
607    */
608   $ldap->search("(&(objectClass=dNSZone)(sOARecord=*))",array("zoneName"));
610   $ForwardZones = array();
611   $ReverseZones = array();
612   $zones = array();
614   while($at = $ldap->fetch()){
615     if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){
616       $ReverseZones[$at['dn']] = $at;
617     }else{
618       $ForwardZones[$at['dn']] = $at;
619     }
620   }
622   foreach($ForwardZones as $dn => $obj){
623     
624     /* try to find reverse
625      */  
626     foreach($ReverseZones as $Rdn => $Robj ){
627       if(preg_match("/".$dn."/",$Rdn)){
628         $zones[$Robj['zoneName'][0]] =  $obj['zoneName'][0];
629       }
630     }
631   }
632   return($zones);
635 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
636 ?>