Code

644c1abe25098ffe913b46460ddae67ee8cca239
[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['pTRRecord']     = "relativeDomainName";// ok
39     $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
40     $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
41     $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
42     $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
43     $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
44     $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok
45     $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
46     $this->RecordTypes['LocRecord']     = "LocRecord";         // ok
47     $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
48     $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
49     $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
50     $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
51     $this->RecordTypes['certRecord']    = "certRecord";        // ok
52     $this->RecordTypes['a6Record']      = "a6Record";          // ok
53     $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
54     $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
55     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
56     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
58     $types = array();
60     /* Get all records */
61     $ldap = $this->config->get_ldap_link();
62     $ldap->cd($this->dn);
63     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
65     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         if(isset($attrs['sOARecord'][0])){
82           $tmp = split("\ ",$attrs['sOARecord'][0]) ;
83           $tmp2 = array();
84           $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
86           /* Assign soa vars */
87           foreach($ar as $key => $name){
88             if(isset($tmp[$key])){
89               $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
90             }else{
91               $types[$attrs['zoneName'][0]][$name] = "";
92             }
93           }
94         }
96         /* Set dns Class*/
97         if(isset($attrs['dNSClass'][0])){
98           $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
99         }
101         /* Set zone Name */
102         if(isset($attrs['zoneName'][0])){
103           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
104         }
106         /* Create list with all used records */
107         foreach($this->RecordTypes as $name => $value){
109           /* If there is a record attribute  */
110           if(isset($attrs[$name])){
112             /* get all entries */
113             for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
114               $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
115                   "inittype"  =>$name,
116                   "value"     =>$attrs[$value][$i],
117                   "status"    =>"edited",
118                   "dn"        =>$attrs['dn']);
119             }
120           }
121         }
122       }
123     }
125     /* If there is at least one entry in this -> types, we have DNS enabled */
126     $this->Zones = $types;
127     if(count($this->Zones) == 0){
128       $this->is_account = false;
129     }else{
130       $this->is_account = true;
131     }
133     /* Store initally account settings */
134     $this->DNSinitially_was_account = $this->is_account;
135   }
138   /* this is used to flip the ip address for example 
139       12.3.45  ->  54.3.12     
140      Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
141       but we want to display 12.3.45.
142   */
143   function FlipIp($ip)
144   {
145     $tmp = array_reverse(split("\.",$ip));
146     $new = "";
147     foreach($tmp as $section){
148       $new .= $section.".";
149     }
150     return(preg_replace("/.$/","",$new));
151   }
153   function execute()
154   {
155     /* Call parent execute */
156     plugin::execute();
158     /* Fill templating stuff */
159     $smarty= get_smarty();
160     $display= "";
162     /* Do we need to flip is_account state? */
163     if (isset($_POST['modify_state'])){
164       $this->is_account= !$this->is_account;
165     }
167     /* Show tab dialog headers */
168     if ($this->is_account){
169       $display= $this->show_header(_("Remove DNS service"),
170           _("This server has DNS features enabled. You can disable them by clicking below."));
171     } else {
172       $display= $this->show_header(_("Add DNS service"),
173           _("This server has DNS features disabled. You can enable them by clicking below."));
174       return ($display);
175     }
177     /* Edited or Added zone hould be saved saved */
178     if(isset($_POST['SaveZoneChanges'])){
179       $this->dialog->save_object();
181       /* Check if noting went wrong */
182       if(count($this->dialog->check())){
183         foreach($this->dialog->check() as $msgs){
184           print_red($msgs); 
185         }
186       }else{
187       
188         /* add new/edited zone */
189         $ret = $this->dialog->save();
190         unset($this->Zones[$this->dialog->InitiallyZoneName]);
191         $this->Zones[$ret['zoneName']]                  = $ret;
192         $this->dialog = NULL;
193       }
194     }
196     /* Cancel zone edit / new */
197     if(isset($_POST['CancelZoneChanges'])){
198       $this->dialog = NULL;
199     }
201     /* Add empty new zone */
202     if(isset($_POST['AddZone'])){
203       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
204     }
206     /* Check for edit zone request */
207     $once = false;
208     foreach( $_POST as $name => $value){
209   
210       /* check all post for edit request */
211       if(preg_match("/^editZone_/",$name)&&!$once){
212         $once =true;
213         $tmp = preg_replace("/^editZone_/","",$name);
214         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
215         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
216       }
218       /* check posts for delete zone */
219       if(preg_match("/^delZone_/",$name)&&!$once){
222         $once =true;
223         $tmp = preg_replace("/^delZone_/","",$name);
224         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
225   
226         $zones =  $this->getUsedZoneNames();
227         $rev = $this->Zones[$tmp]['ReverseZone'];
228         $res = array_merge(($zones[$tmp]),($zones[$rev.".in-addr.arpa"]));
229         
230         if(count($res)){
231           $i = 2;
232           $str ="";
233           foreach($res as $dn){
234             if($i > 0 ){
235               $i --;
236               $str.=$dn." ";
237             }
238           }
239           if(count($res)> 2) $str .=" ... ";
240           print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
241         }else{
242           unset($this->Zones[$tmp]);
243         }
244       }
245     }
247     /* Show dialog */
248     if($this->dialog!= NULL){
249       $this->dialog->save_object();
250       $this->dialog->parent = $this;
251       return($this->dialog->execute());
252     }
254     /* Create Listbox with existing Zones */
255     $ZoneList = new divSelectBox("dNSZones");
256     $ZoneList -> SetHeight(254);
258     /* Add entries to divlist*/
259     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
260       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
261     foreach($this->Zones as $zone => $values ){
262       $ZoneList->AddEntry(array(
263             array("string" => $zone),
264             array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
265             array("string" => _("TTL")." : ".$values['sOAttl']),
266             array("string" => _("Class")." : ".$values['dNSClass']),
267             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
268             ));
269     }    
271     /* Display tempalte */
272     $smarty->assign("ZoneList",$ZoneList->DrawList());
273     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
274     return($display);
275   }
278   /* This funtion returns all used Zonenames */
279   function getUsedZoneNames()
280   {
281     $ret = array();
282     $ldap = $this->config->get_ldap_link();
283     $ldap->cd($this->config->current['BASE']);
284     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName","tXTRecord"));
285     while($attr = $ldap->fetch()){
286       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
287         $ret[$attr['zoneName'][0]][] = $attr['dn'];
288       }else{
289         $ret[$attr['zoneName'][0]][] = $attr['dn'];
290       }
291     }
292     return($ret);
293   }
296   /* Remove dns service */
297   function remove_from_parent()
298   {
299     if(!$this->DNSinitially_was_account){
300       return;
301     }
303     $ldap = $this->config->get_ldap_link();
304     $ldap->cd($this->config->current['BASE']);
305     foreach($this->usedDNS as $dn){
306       $ldap->cd($dn);
307       $ldap->rmdir_recursive($dn);
308     }
310     $ldap = $this->config->get_ldap_link();
311     $ldap->cd($this->orig_dn);
312     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",array("relativeDomainName","zoneName"));
313     while($attr = $ldap->fetch()){
314       $ldap->cd($attr['dn']);
315       $ldap->rmDir($attr['dn']);
316     }
319     show_ldap_error($ldap->get_error());
320   }
323   /* Save data to object */
324   function save_object()
325   {
326   }
329   /* Check supplied data */
330   function check()
331   {
332     $message= array();
333     return ($message);
334   }
337   /* Save to LDAP */
338   function save()
339   {
340     /* Ldap conenction / var initialization */
341     $ldap = $this->config->get_ldap_link();
342     $ldap->cd($this->config->current['BASE']);
343     $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
345     /* Generate entries for all zones, and check if they must be updated deleted added */
346     foreach($this->Zones as $zone){
347       
348       /* Get ldap syntax */
349       $tmp = $this->generate_LDAP_entries($zone);
351       /* Check if dn is new, or if entry was edited */
352       foreach($tmp as $key => $values){
353         if(isset($this->usedDNS[$key])){
354           $actions['update'][$key]=$values;
355           unset($this->usedDNS[$key]);
356         }else{
357           $actions['add'][$key] = $values;
358         }
359       }
360     }
362     /* Check which dns are not used anymore ...*/
363     foreach($this->usedDNS as $key => $values){
364       $actions['delete'][$key] = $values;
365     }
367     /* Remove deleted zones */
368     foreach($actions['delete'] as $dn => $attrs){
369       $ldap->cd($dn);
370       $ldap->rmdir_recursive($dn);
371     }
373     /* Add new zones */
374     foreach($actions['add'] as $dn => $attrs){
375       $ldap->cd($this->config->current['BASE']);
376       //      $ldap->create_missing_trees($dn);
377       $ldap->cd($dn);
378       $ldap->add($attrs);
379     }
381     /* Update existing entries */
382     foreach($actions['update'] as $dn => $attrs){
383       $ldap->cd($dn);
384       //$this->cleanup();
385       $ldap->modify ($attrs); 
387     }
388     show_ldap_error($ldap->get_error());
389   }
392   /* This function generates ldap friendly output 
393      of all changes for a single zone (reverse and forward)
394    */
395   function generate_LDAP_entries($zone)
396   {
397     $tmp = array();
398     $tmp['objectClass']           = array("top","dNSZone");
399     $tmp['dNSClass']              = "IN";//$zone['dNSClass']; 
400     $tmp['relativeDomainName']    = "@";//$zone['relativeDomainName']; 
402     $str = "";
403     foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
404       $str .= $zone[$name]." "; 
405     }
406     $tmp['sOARecord'] = $str;
409     /* Generate Record entries  */
410     $arr = array("aRecord","SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
411         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
412     $aRecords = array();
413     foreach($arr as $ar){
414       if((isset($zone['Records']))&&(is_array($zone['Records']))){
415         foreach($zone['Records'] as $type){
416           if(($type['type'] == $ar)&&($type['status']!="deleted")){
417             $tmp[$ar][] = $type['value'];
418           }
419         }
420       }
421     }
422     /* Check if there are records removed,
423         if there are some removed records, the append an array        
424         to ensure that these record types are deleted 
425      */
426     if((isset($zone['Records']))&&(is_array($zone['Records']))){
427       foreach($zone['Records'] as $type){
428         if((isset($type['inittype']))&&($type['inittype']!="")){
429           if($type['type'] != $type['inittype']){
430             $tmp[$type['inittype']] = array();
431           }
432         }
433       }
434     }
435    
436     /* generate forward entry */
437     $dn = "zoneName=".$zone['zoneName'].",".$this->dn; 
438     $tmp2[$dn] = $tmp;
439     $tmp2[$dn]['zoneName'] = $zone['zoneName'];
441     /* generate reverse entry */
442     $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
443     $tmp2[$dn] = $tmp;
444     $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
445     $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
447     return($tmp2);
448   }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>