Code

fb0cd32417a96bc29fa8a7cb60d51f424adf829c
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 class servdns extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = FALSE;
12   var $attributes       = array(); 
13   var $objectclasses    = array("whatever");
15   var $RecordTypes      = array();
17   var $Zones  = array();
18   var $dialog = NULL;
20   var $usedDNS    = array();
22   var $orig_dn = "";
24   var $DNSinitially_was_account;
27   function servdns ($config, $dn= NULL)
28   {
29     plugin::plugin ($config, $dn);
31     $this->orig_dn = $dn;
33     /* All types with required attrs */
34     $this->RecordTypes['aRecord']       = "aRecord";           // ok
35     $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
36     $this->RecordTypes['mXRecord']      = "mXRecord";          // ok
37     $this->RecordTypes['nSRecord']      = "nSRecord";          // ok
38     $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
39     $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
40 //    $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
41     $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
42     $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
43     $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok
44     $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
45     $this->RecordTypes['LocRecord']     = "LocRecord";         // ok
46     $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
47     $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
48     $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
49     $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
50     $this->RecordTypes['certRecord']    = "certRecord";        // ok
51     $this->RecordTypes['a6Record']      = "a6Record";          // ok
52     $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
53     $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
54     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
55     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
57     $types = array();
59     /* Get all records */
60     $ldap = $this->config->get_ldap_link();
61     $ldap->cd($this->dn);
62     $ldap->ls("(&(objectClass=dNSZone)(relativeDomainName=@))",$dn,array("*"));
64     while($attrs = $ldap->fetch()){
66       /* If relative domainname 
67        * Try to read dnsclass / TTl / zone
68        */
69       $this->usedDNS[$attrs['dn']] = $attrs['dn'];
70       if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
71         $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
72         $z = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
73         
74         $z = $this->FlipIp($z);
76         $types[$zoneName]['ReverseZone']  = $z;
77         $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
78       }else{
80         /* Generate SOA entry 
81          */
82         if(isset($attrs['sOARecord'][0])){
83           $tmp = split("\ ",$attrs['sOARecord'][0]) ;
84           $tmp2 = array();
85           $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
87           /* Assign soa vars */
88           foreach($ar as $key => $name){
89             if(isset($tmp[$key])){
90               $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
91             }else{
92               $types[$attrs['zoneName'][0]][$name] = "";
93             }
94           }
95         }
97         /* Set dns Class
98          */
99         if(isset($attrs['dNSClass'][0])){
100           $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
101         }
103         /* Set zone Name 
104          */
105         if(isset($attrs['zoneName'][0])){
106           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
107         }
109         /* Create list with all used records 
110          */
111         foreach($this->RecordTypes as $name => $value){
113           /* If there is a record attribute  
114            */
115           if(isset($attrs[$name])){
117             /* get all entries 
118              */
119             for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
120               $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
121                   "inittype"  =>$name,
122                   "value"     =>$attrs[$value][$i],
123                   "status"    =>"edited",
124                   "dn"        =>$attrs['dn']);
125             }
126           }
127         }
128       }
129     }
131     /* If there is at least one entry in this -> types, we have DNS enabled 
132      */
133     $this->Zones = $types;
134     if(count($this->Zones) == 0){
135       $this->is_account = false;
136     }else{
137       $this->is_account = true;
138     }
140     /* Store initally account settings 
141      */
142     $this->DNSinitially_was_account = $this->is_account;
143   }
146   /* this is used to flip the ip address for example 
147       12.3.45  ->  54.3.12     
148      Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
149       but we want to display 12.3.45.
150   */
151   function FlipIp($ip)
152   {
153     $tmp = array_reverse(split("\.",$ip));
154     $new = "";
155     foreach($tmp as $section){
156       $new .= $section.".";
157     }
158     return(preg_replace("/.$/","",$new));
159   }
161   function execute()
162   {
163     /* Call parent execute 
164      */
165     plugin::execute();
167     /* Fill templating stuff 
168      */
169     $smarty= get_smarty();
170     $display= "";
172     /* Do we need to flip is_account state? 
173      */
174     if (isset($_POST['modify_state'])){
175       $this->is_account= !$this->is_account;
176     }
178     /* Show tab dialog headers 
179      */
180     if ($this->is_account){
181       $display= $this->show_header(_("Remove DNS service"),
182           _("This server has DNS features enabled. You can disable them by clicking below."));
183     } else {
184       $display= $this->show_header(_("Add DNS service"),
185           _("This server has DNS features disabled. You can enable them by clicking below."));
186       return ($display);
187     }
189     /* Edited or Added zone hould be saved saved 
190      */
191     if(isset($_POST['SaveZoneChanges'])){
192       $this->dialog->save_object();
194       /* Check if noting went wrong 
195        */
196       if(count($this->dialog->check())){
197         foreach($this->dialog->check() as $msgs){
198           print_red($msgs); 
199         }
200       }else{
201       
202         /* add new/edited zone 
203          */
204         $ret = $this->dialog->save();
205         $ret['InitialzoneName']= $this->dialog->InitiallyZoneName;
206         $ret['InitialreverseZone']= $this->dialog->InitiallyReverseZone;
207         unset($this->Zones[$this->dialog->InitiallyZoneName]);
208         $this->Zones[$ret['zoneName']]                  = $ret;
209         $this->dialog = NULL;
210       }
211     }
213     /* Cancel zone edit / new 
214      */
215     if(isset($_POST['CancelZoneChanges'])){
216       $this->dialog = NULL;
217     }
219     /* Add empty new zone 
220      */
221     if(isset($_POST['AddZone'])){
222       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
223     }
225     /* Check for edit zone request 
226      */
227     $once = false;
228     foreach( $_POST as $name => $value){
229   
230       /* check all post for edit request 
231        */
232       if(preg_match("/^editZone_/",$name)&&!$once){
233         $once =true;
234         $tmp = preg_replace("/^editZone_/","",$name);
235         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
236         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
237       }
239       /* check posts for delete zone 
240        */
241       if(preg_match("/^delZone_/",$name)&&!$once){
242         $once =true;
243         $tmp = preg_replace("/^delZone_/","",$name);
244         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
245   
246         $zones =  $this->getUsedZoneNames();
247     
248         if(isset($this->Zones[$tmp]['InitialreverseZone'])){
249           $rev = $this->FlipIp($this->Zones[$tmp]['InitialreverseZone']);
250         }else{
251           $rev = $this->FlipIp($this->Zones[$tmp]['ReverseZone']);
252         }
254         if(isset($this->Zones[$tmp]['InitialzoneName'])){
255           $tmp= $this->Zones[$tmp]['InitialzoneName'];
256         }
258         $res = array_merge(($zones[$tmp]),($zones[$rev.".in-addr.arpa"]));
259         
260         if(count($res)){
261           $i = 2;
262           $str ="";
263           foreach($res as $dn){
264             if($i > 0 ){
265               $i --;
266               $str.=$dn." ";
267             }
268           }
269           if(count($res)> 2) $str .=" ... ";
270           print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
271         }else{
272 //          unset($this->Zones[$tmp]);
273         }
274       }
275     }
277     /* Show dialog 
278      */
279     if($this->dialog!= NULL){
280       $this->dialog->save_object();
281       $this->dialog->parent = $this;
282       return($this->dialog->execute());
283     }
285     /* Create Listbox with existing Zones 
286      */
287     $ZoneList = new divSelectBox("dNSZones");
288     $ZoneList -> SetHeight(254);
290     /* Add entries to divlist
291      */
292     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
293       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
294     foreach($this->Zones as $zone => $values ){
295       $ZoneList->AddEntry(array(
296             array("string" => $zone),
297             array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
298             array("string" => _("TTL")." : ".$values['sOAttl']),
299             array("string" => _("Class")." : ".$values['dNSClass']),
300             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
301             ));
302     }    
304     /* Display tempalte 
305      */
306     $smarty->assign("ZoneList",$ZoneList->DrawList());
307     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
308     return($display);
309   }
312   /* This funtion returns all used Zonenames 
313    */
314   function getUsedZoneNames()
315   {
316     $ret = array();
317     $ldap = $this->config->get_ldap_link();
318     $ldap->cd($this->config->current['BASE']);
319     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName","tXTRecord"));
320     while($attr = $ldap->fetch()){
321       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
322         $ret[$attr['zoneName'][0]][] = $attr['dn'];
323       }else{
324         $ret[$attr['zoneName'][0]][] = $attr['dn'];
325       }
326     }
327     return($ret);
328   }
331   /* Remove dns service 
332    */
333   function remove_from_parent()
334   {
335     if(!$this->DNSinitially_was_account){
336       return;
337     }
339     $ldap = $this->config->get_ldap_link();
340     $ldap->cd($this->config->current['BASE']);
341     foreach($this->usedDNS as $dn){
342       $ldap->cd($dn);
343       $ldap->rmdir_recursive($dn);
344     }
346     $ldap = $this->config->get_ldap_link();
347     $ldap->cd($this->orig_dn);
348     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",array("relativeDomainName","zoneName"));
349     while($attr = $ldap->fetch()){
350       $ldap->cd($attr['dn']);
351       $ldap->rmDir($attr['dn']);
352     }
355     show_ldap_error($ldap->get_error());
356   }
359   /* Save data to object */
360   function save_object()
361   {
362   }
365   /* Check supplied data */
366   function check()
367   {
368     $message= array();
369     return ($message);
370   }
373   /* Save to LDAP */
374   function save()
375   {
376     /* Ldap conenction / var initialization 
377      */
378     $ldap = $this->config->get_ldap_link();
379     $ldap->cd($this->config->current['BASE']);
380     $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
382     /* Generate entries for all zones, and check if they must be updated deleted added 
383      */
384     foreach($this->Zones as $key =>  $zone){
385       if(isset($zone['InitialzoneName']) && ($zone['zoneName'] != $zone['InitialzoneName'])){
386         $dst = "zoneName=".$zone['zoneName'].",".$this->dn;
387         $src = "zoneName=".$zone['InitialzoneName'].",".$this->dn;
388         $this->recursive_move($src,$dst);
390         $tmp['zoneName'] = $zone['zoneName'];
391         $ldap->search ("(&(objectClass=dNSZone)(zoneName=".$zone['InitialzoneName']."))",array("relativeDomainName"));
392         while($attrs = $ldap->fetch()){
393           $ldap->cd($attrs['dn']);
394           $ldap->modify($tmp);
395           show_ldap_error($ldap->get_error()); 
396         }
397       }
398       if(isset($zone['InitialreverseZone']) &&($zone['InitialreverseZone'] != $zone['ReverseZone'])){
399         $dst = "zoneName=".$zone['ReverseZone'].",".$this->dn;
400         $src = "zoneName=".$zone['InitialreverseZone'].",".$this->dn;
401         $this->recursive_move($src,$dst);
402         
403         $tmp['zoneName'] = $zone['zoneName'];
404         $ldap->search ("(&(objectClass=dNSZone)(zoneName=".$zone['InitialzoneName']."))",array("relativeDomainName"));
405         while($attrs = $ldap->fetch()){
406           $ldap->cd($attrs['dn']);
407           $ldap->modify($tmp);
408           show_ldap_error($ldap->get_error()); 
409         }
410       }  
411       
412  
413       /* Get ldap syntax 
414        */
415       $tmp = $this->generate_LDAP_entries($zone);
417       /* Check if dn is new, or if entry was edited 
418        */
419       foreach($tmp as $key => $values){
420         if(isset($this->usedDNS[$key])){
421           $actions['update'][$key]=$values;
422           unset($this->usedDNS[$key]);
423         }else{
424           $actions['add'][$key] = $values;
425         }
426       }
427     }
429     /* Check which dns are not used anymore ...
430      */
431     foreach($this->usedDNS as $key => $values){
432       $actions['delete'][$key] = $values;
433     }
435     /* Remove deleted zones 
436      */
437     foreach($actions['delete'] as $dn => $attrs){
438       $ldap->cd($dn);
439       $ldap->rmdir_recursive($dn);
440     }
442     /* Add new zones 
443      */
444     foreach($actions['add'] as $dn => $attrs){
445       $ldap->cd($this->config->current['BASE']);
446       $ldap->cd($dn);
447       $ldap->add($attrs);
448     }
450     /* Update existing entries 
451      */
452     foreach($actions['update'] as $dn => $attrs){
453       $ldap->cd($dn);
454       $ldap->modify ($attrs); 
456     }
458     show_ldap_error($ldap->get_error());
459   }
462   /* This function generates ldap friendly output 
463      of all changes for a single zone (reverse and forward)
464    */
465   function generate_LDAP_entries($zone)
466   {
467     $tmp = array();
468     $tmp['objectClass']           = array("top","dNSZone");
469     $tmp['dNSClass']              = "IN";//$zone['dNSClass']; 
470     $tmp['relativeDomainName']    = "@";//$zone['relativeDomainName']; 
472     $str = "";
473     foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
474       $str .= $zone[$name]." "; 
475     }
476     $tmp['sOARecord'] = $str;
479     /* Generate Record entries  
480      */
481     $arr = array("aRecord","SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
482         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
483     $aRecords = array();
484     foreach($arr as $ar){
485       if((isset($zone['Records']))&&(is_array($zone['Records']))){
486         foreach($zone['Records'] as $type){
487           if(($type['type'] == $ar)&&($type['status']!="deleted")){
488             $tmp[$ar][] = $type['value'];
489           }
490         }
491       }
492     }
493     /* Check if there are records removed,
494         if there are some removed records, then append an array        
495         to ensure that these record types are deleted 
496      */
497     if((isset($zone['Records']))&&(is_array($zone['Records']))){
498       foreach($zone['Records'] as $type){
499         if((isset($type['inittype']))&&($type['inittype']!="")){
500           if($type['type'] != $type['inittype']){
501             $tmp[$type['inittype']] = array();
502           }
503         }
504       }
505     }
506    
507     /* generate forward entry 
508      */
509     $dn = "zoneName=".$zone['zoneName'].",".$this->dn; 
510     $tmp2[$dn] = $tmp;
511     $tmp2[$dn]['zoneName'] = $zone['zoneName'];
513     /* generate reverse entry 
514      */
515     $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
516     $tmp2[$dn] = $tmp;
517     $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
518     $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
520     return($tmp2);
521   }
523 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
524 ?>