Code

adc3aa6730286c8a310256652a2e9999e552238b
[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();
21   function servdns ($config, $dn= NULL)
22   {
23     plugin::plugin ($config, $dn);
25     /* All types with required attrs */
26     $this->RecordTypes['aRecord']       = "aRecord";           // ok
27     $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
28     $this->RecordTypes['mXRecord']      = "mXRecord";          // ok
29     $this->RecordTypes['nSRecord']      = "nSRecord";          // ok
30     $this->RecordTypes['pTRRecord']     = "relativeDomainName";// ok
31     $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
32     $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
33     $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
34     $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
35     $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
36     $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok
37     $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
38     $this->RecordTypes['LocRecord']     = "LocRecord";         // ok
39     $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
40     $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
41     $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
42     $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
43     $this->RecordTypes['certRecord']    = "certRecord";        // ok
44     $this->RecordTypes['a6Record']      = "a6Record";          // ok
45     $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
46     $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
47     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
48     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
50     $this->cn = $this->attrs['cn'][0];
52     $types = array();
54     /* Get all records */
55     $ldap = $this->config->get_ldap_link();
56     $ldap->cd($this->dn);
57     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
59     while($attrs = $ldap->fetch()){
60       /* If relative domainname == cn
61        * Try to read dnsclass / TTl / zone
62        */
63       $this->usedDNS[$attrs['dn']] = $attrs['dn'];
64       if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
65         $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
66         $types[$zoneName]['ReverseZone']  = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
67         $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
68       }else{
70         /* Generate SOA entry */
71         if(isset($attrs['sOARecord'][0])){
72           $tmp = split("\ ",$attrs['sOARecord'][0]) ;
73           $tmp2 = array();
74           $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
76           /* Assign soa vars */
77           foreach($ar as $key => $name){
78             if(isset($tmp[$key])){
79               $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
80             }else{
81               $types[$attrs['zoneName'][0]][$name] = "";
82             }
83           }
84         }
86         /* Set TTL value */
87         if(isset($attrs['dNSTTL'][0])){
88           $types[$attrs['zoneName'][0]]['dNSTTL'] = $attrs['dNSTTL'][0];
89         }
91         /* Set dns Class*/
92         if(isset($attrs['dNSClass'][0])){
93           $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
94         }
96         /* Set zone Name */
97         if(isset($attrs['zoneName'][0])){
98           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
99         }
101         /* Create list with all used records */
102         foreach($this->RecordTypes as $name => $value){
104           /* If there is a record attribute  */
105           if(isset($attrs[$name])){
107             $types[$attrs['zoneName'][0]]['Records']=array();
109             /* get all entries */
110             for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
111               $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
112                   "inittype"  =>$name,
113                   "value"     =>$attrs[$value][$i],
114                   "status"    =>"edited",
115                   "dn"        =>$attrs['dn']);
116             }
117           }
118         }
119       }
120     }
122     /* If there is at least one entry in this -> types, we have DNS enabled */
123     $this->Zones = $types;
124     if(count($this->Zones) == 0){
125       $this->is_account = false;
126     }else{
127       $this->is_account = true;
128     }
130     /* Store initally account settings */
131     $this->DNSinitially_was_account = $this->is_account;
132   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     /* Fill templating stuff */
140     $smarty= get_smarty();
141     $display= "";
143     /* Do we need to flip is_account state? */
144     if (isset($_POST['modify_state'])){
145       $this->is_account= !$this->is_account;
146     }
148     /* Show tab dialog headers */
149     if ($this->is_account){
150       $display= $this->show_header(_("Remove DNS service"),
151           _("This server has DNS features enabled. You can disable them by clicking below."));
152     } else {
153       $display= $this->show_header(_("Add DNS service"),
154           _("This server has DNS features disabled. You can enable them by clicking below."));
155       return ($display);
156     }
158     if(isset($_POST['SaveZoneChanges'])){
159       $this->dialog->save_object();
161       if(count($this->dialog->check())){
162         foreach($this->dialog->check() as $msgs){
163           print_red($msgs); 
164         }
165       }else{
167         $ret = $this->dialog->save();
168         unset($this->Zones[$this->dialog->InitiallyZoneName]);
169         $this->Zones[$ret['zoneName']]                  = $ret;
170         $this->dialog = NULL;
171       }
172     }
174     /* Cancel zone edit / new */
175     if(isset($_POST['CancelZoneChanges'])){
176       $this->dialog = NULL;
177     }
179     /* Add empty new zone */
180     if(isset($_POST['AddZone'])){
181       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
182     }
184     /* Check for edit zone request */
185     $once = false;
186     foreach( $_POST as $name => $value){
188       if(preg_match("/^editZone_/",$name)&&!$once){
189         $once =true;
190         $tmp = preg_replace("/^editZone_/","",$name);
191         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
192         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
193       }
195       if(preg_match("/^delZone_/",$name)&&!$once){
196         $once =true;
197         $tmp = preg_replace("/^delZone_/","",$name);
198         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
199         unset($this->Zones[$tmp]);
200       }
201     }
203     if($this->dialog!= NULL){
204       $this->dialog->save_object();
205       $this->dialog->parent = $this;
206       return($this->dialog->execute());
207     }
209     /* Create Listbox with existing Zones */
210     $ZoneList = new divSelectBox("dNSZones");
211     $ZoneList -> SetHeight(254);
213     /* Add entries to divlist*/
214     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
215       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
216     foreach($this->Zones as $zone => $values ){
217       $ZoneList->AddEntry(array(
218             array("string" => $zone),
219             array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
220             array("string" => _("TTL")." : ".$values['dNSTTL']),
221             array("string" => _("Class")." : ".$values['dNSClass']),
222             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
223             ));
224     }    
226     /* Display tempalte */
227     $smarty->assign("ZoneList",$ZoneList->DrawList());
228     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
229     return($display);
230   }
232   function remove_from_parent()
233   {
234     $ldap = $this->config->get_ldap_link();
235     $ldap->cd($this->config->current['BASE']);
236     foreach($this->usedDNS as $dn){
237       $ldap->cd($dn);
238       $ldap->rmdir_recursive($dn);
239     }
240     show_ldap_error($ldap->get_error());
241   }
244   /* Save data to object */
245   function save_object()
246   {
247   }
250   /* Check supplied data */
251   function check()
252   {
253     $message= array();
254     return ($message);
255   }
258   /* Save to LDAP */
259   function save()
260   {
261     $ldap = $this->config->get_ldap_link();
262     $ldap->cd($this->config->current['BASE']);
264     $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
266     foreach($this->Zones as $zone){
267       $tmp = $this->generate_LDAP_entries($zone);
268       foreach($tmp as $key => $values){
269         if(isset($this->usedDNS[$key])){
270           $actions['update'][$key]=$values;
271           unset($this->usedDNS[$key]);
272         }else{
273           $actions['add'][$key] = $values;
274         }
275       }
276     }
277     foreach($this->usedDNS as $key => $values){
278       $actions['delete'][$key] = $values;
279     }
281     foreach($actions['delete'] as $dn => $attrs){
282       $ldap->cd($dn);
283       $ldap->rmdir_recursive($dn);
284     }
286     foreach($actions['add'] as $dn => $attrs){
287       $ldap->cd($this->config->current['BASE']);
288       //      $ldap->create_missing_trees($dn);
289       $ldap->cd($dn);
290       $ldap->add($attrs);
291     }
293     foreach($actions['update'] as $dn => $attrs){
294       $ldap->cd($dn);
295       $ldap->modify($attrs);
296     }
297     show_ldap_error($ldap->get_error());
298   }
301   function generate_LDAP_entries($zone)
302   {
303     $tmp = array();
304     $tmp['objectClass']           = array("top","dNSZone");
305     $tmp['dNSTTL']                = $zone['dNSTTL']; 
306     $tmp['dNSClass']              = $zone['dNSClass']; 
307     $tmp['relativeDomainName']    = "@";//$zone['relativeDomainName']; 
309     $str = "";
310     foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
311       $str .= $zone[$name]." "; 
312     }
313     $tmp['sOARecord'] = $str;
316     /* Generate some attrs  */
317     $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
318         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
319     $aRecords = array();
320     foreach($arr as $ar){
321       if((isset($zone['Records']))&&(is_array($zone['Records']))){
322         foreach($zone['Records'] as $type){
323           if(($type['type'] == $ar)&&($type['status']!="deleted")){
324             $tmp[$ar][] = $type['value'];
325           }
326         }
327       }
328     }
329     if((isset($zone['Records']))&&(is_array($zone['Records']))){
330       foreach($zone['Records'] as $type){
331         if(isset($type['inittype'])){
332           if($type['type'] != $type['inittype']){
333             $tmp[$type['inittype']] = array();
334           }
335         }
336       }
337     }
338     $dn = "zoneName=".$zone['zoneName'].",".$this->dn; 
339     $tmp2[$dn] = $tmp;
340     $tmp2[$dn]['zoneName'] = $zone['zoneName'];
341     $dn = "zoneName=".$zone['ReverseZone'].".in-addr.arpa,".$this->dn;
342     $tmp2[$dn] = $tmp;
343     $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
344     $tmp2[$dn]['zoneName'] = $zone['ReverseZone'].".in-addr.arpa";
346     return($tmp2);
347   }
353 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>