Code

added new zone management (zone editor is not updated yet)
[gosa.git] / plugins / admin / systems / class_termDNS.inc
1 <?php
3 class termDNS 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 = TRUE;
13   /* Basic informations 
14    */
15   var $attributes     = array("ipHostNumber","macAddress");
16   var $objectclasses  = array("whatever");
18   var $ipHostNumber   = "";    // IP address 
19   var $macAddress     = "";    // Mac address 
20   var $cn             = "";    // CN of currently edited device 
21   var $OrigCn         = "";    // Initial cn
22   var $IPisMust       = false;
23   var $MACisMust      = false;
25   /* DNS attributes  
26    */
27   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
28   var $DNS_is_Account           = false;
29   var $DNSinitially_was_account = false;
30   var $dnsEntry                 = array();
31   var $DNSenabled               = false;
33   /*  Terminal dns 
34    */
35   function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
36   {
37     /* We need to know which objectClasses are used, to store the ip/mac
38      * Because of different type of devices   
39      */
40     $this->objectclasses  =  $objectClasses;
41     $this->IPisMust       = $IPisMust;
43     plugin::plugin ($config, $dn);
45     $this->OrigCn = $this->attrs['cn'][0];
47  
48     /* Hide all dns specific code, if dns is not available 
49      */
50     $DNSenabled = false;
51     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
52       if(preg_match("/^servdns$/",$tab['CLASS'])){
53         $this->DNSenabled = true;
54       }
55     }
56     if(!$this->DNSenabled){
57       $this->DNS_is_account = false;
58       return;
59     }
61     /* Get Zones  
62      */
63     $this->Zones        = getAvailableZones($config);
65     /* Get Entry 
66      */
67     $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
69     /* Remove A record which equals $this->ipHostNumber
70      */
71     foreach($this->dnsEntry['RECORDS'] as $key => $rec){
72       if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
73         unset($this->dnsEntry['RECORDS'][$key]);
74       }
75     }
77     /* Get Record types 
78      */
79     $this->RecordTypes  = getDnsRecordTypes();
81     /* If there is at least one entry in this -> types, we have DNS enabled 
82      */
83     if($this->dnsEntry['exists']){
84       $this->DNS_is_account = true;
85     }else{
86       $this->DNS_is_account = false;
87     }
88     
89     /* Store initally account settings 
90      */
91     $this->DNSinitially_was_account = $this->DNS_is_account;
92   }
95   function getVarsForSaving($attrs) 
96   {
97     foreach($this->attributes as $attr){
98       if(!empty($this->$attr)){
99         $attrs[$attr] = $this->$attr;
100       }
101     }
102     return($attrs); 
103   }
105   function execute()
106   {
107           /* Call parent execute */
108     $smarty= get_smarty();
109     $display= "";
111     $smarty->assign("staticAddress", ""); 
112  
113     /* There is no dns available 
114      */
115     if($this->DNSenabled == false){
116        
117       /* Is IP address must ? */ 
118       $smarty->assign("DNS_is_account",false);  
119       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
120     
121       /* Assign smarty all non DNs attributes */
122       foreach($this->attributes as $attr){
123         $smarty->assign($attr,$this->$attr);
124       }
125       $smarty->assign("staticAddress", "");
126       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
127       return($display);
128     }else{
129       $smarty->assign("DNS_is_account",true); 
130     }
131  
132     /* Add new empty array to our record list */
133     if(isset($_POST['AddNewRecord'])){
134       $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
135     }
136    
137     /* Handle all posts */ 
138     $only_once =true;
139     foreach($_POST as $name => $value){
141       /* Check if we have to delete a record entry */
142       if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
143       
144         /* Avoid performing this once again */
145         $only_once = false;
147         /* Extract id for specified entry */
148         $id = preg_replace("/RemoveRecord_/","",$name);
149         $id = preg_replace("/_.*$/","",$id);
150     
151         /* Delete this record, mark edited entries to be able to delete them */
152         if(isset($this->dnsEntry['RECORDS'][$id])){
153           unset($this->dnsEntry['RECORDS'][$id]);
154         }
155       }
156     }
158     /* Assign smarty all non DNs attributes */
159     foreach($this->attributes as $attr){
160       $smarty->assign($attr,$this->$attr);
161     }
163     /* Assign smarty all DNS attributes */
164     foreach($this->DNSattributes as $attr){
165       $smarty->assign($attr,$this->dnsEntry[$attr]);
166     }
167     
168     /* Assign all needed vars */
169     $smarty->assign("DNSAccount",$this->DNS_is_account);
170     $smarty->assign("Zones",$this->Zones);
171     $smarty->assign("ZoneKeys",($this->Zones));
172     $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
173   
174     $tmp = $this->generateRecordsList();
175     
176     $changeStateForRecords = $tmp['changeStateForRecords'];
178     $smarty->assign("records",$tmp['str']);
179     $smarty->assign("changeStateForRecords",$changeStateForRecords);
180     $smarty->assign("staticAddress","<font class=\"must\">*</font>");
182     $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
183     return($display);
184   }
186   function remove_from_parent()
187   {
188     /*
189     $ldap = $this->config->get_ldap_link();
190     $ldap->cd($this->orig_dn);
191     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
192     while($attr = $ldap->fetch()){  
193       $ldap->cd($attr['dn']);
194       $ldap->rmDir($attr['dn']);
195     }
196     */
197   }
199   /* Save data to object */
200   function save_object()
201   {
202     /* Save all posted vars */
203     plugin::save_object();
204     
205     /* Ge all non dns attributes (IP/MAC)*/
206     foreach($this->attributes as $attr){
207       if(isset($_POST[$attr])){
208         $this->$attr = $_POST[$attr];
209       }
210     }
212     /* Get dns attributes */
213     if(isset($_POST['network_tpl_posted'])){
215       /* Check for posted record changes */
216       foreach($this->dnsEntry['RECORDS'] as $key => $value){
217       
218         /* Check if type has changed */
219         if(isset($_POST['RecordTypeSelectedFor_'.$key])){
220           $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
221         }
222         /* Check if value has changed */
223         if(isset($_POST['RecordValue_'.$key])){
224           $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
225         }
226       }
227       
228       /* Get all basic DNS attributes (TTL, Clas ..)*/
229       foreach($this->DNSattributes as $attr){
230         if(isset($_POST[$attr])){
231           $this->dnsEntry[$attr] = $_POST[$attr];
232         }
233       }
235       /* Enable diable DNS */
236       if(isset($_POST['enableDNS'])){
237         $this->DNS_is_account = true;
238       }else{
239         $this->DNS_is_account = false;
240       }
241     }
242   }
245   /* Check supplied data */
246   function check()
247   {
248     $message= array();
250     /* Check if ip must be given
251      */  
252     if(($this->IPisMust)||($this->DNS_is_account)){
253   
254       /* Check if ip is empty 
255        */
256       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
257         $message[]= _("The required field 'IP-address' is not set.");
258       }
260       /* check if given ip is valid ipi
261        */
262       $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
263       if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
264         $message[]= _("Wrong IP format in field IP-address.");
265       }
266     }
268     /* Check if mac is empty 
269      */
270     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
271       $message[]= _("The required field 'MAC-address' is not set.");
272     }
274     /* Check if given mac is valid mac 
275      */
276     $tr = count(split(":",$this->macAddress));
277     if($tr!=6){
278       $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
279     }
281     /* only perfrom this checks if this is a valid DNS account */
282     if($this->DNS_is_account){
284       $checkArray = array();
285       $onlyOnce   = array();
287       $onlyOnce['cNAMERecord'] = 0;
289       /* Walk through all entries and detect duplicates or mismatches
290        */  
291       foreach($this->dnsEntry['RECORDS'] as $name => $values){
293         /* Count record values, to detect duplicate entries for a specific record
294          */
295         if(!isset($checkArray[$values['type']][$values['value']])){
296           $checkArray[$values['type']][$values['value']] = 0;
297         }else{
298           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
299         }
301         /* Check if given entries in $onlyOnce are used more than once
302          */
303         if(isset($onlyOnce[$values['type']])){
304           $onlyOnce[$values['type']] ++;
305           if($onlyOnce[$values['type']] > 1){
306             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
307           }
308         }
310         /* Skip txt record ... 
311          */
312         if($values['type'] == "tXTRecord") continue;
314         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
315          */
316         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
317           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
318                $this->ipHostNumber);
319         }
321         /* only lower-case is allowed in record entries ... 
322          */
323         if($values['value'] != strtolower($values['value'])){
324           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
325         }
326       }
327     }
328     return ($message);
329   }
332   /* Save to LDAP */
333   function save($dn)
334   {
335     $ldap= $this->config->get_ldap_link();
336    
337     /*******************/ 
338     /* IP-MAC HANDLING */
339     /*******************/ 
341     /* $dn was posted as parameter */
342     $this->dn = $dn;
343     
344     /* Save DNS setting & ip/Mac*/
345     plugin::save();
347     /* Write back to ldap */
348     $ldap->cd($this->dn);
349     $this->cleanup();
350     $ldap->modify ($this->attrs); 
352     /****************/ 
353     /* DNS HANDLING */
354     /****************/ 
356     /* If isn't DNS account but initially was DNS account 
357        remove all DNS entries 
358      */ 
360     /* Add ipHostNumber to aRecords
361      */
362     $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
364     /* Create diff and follow instructions 
365      * If Account was disabled, remove account by setting exists to false
366      */
367     if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
368       $this->dnsEntry['exists'] = false;
369       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
370     }else{
371       $this->dnsEntry['exists'] = $this->DNS_is_account;
372       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
373     }   
375     /* Delete dns */
376     foreach($tmp['del'] as $dn => $del){
377       $ldap->cd($dn);
378       $ldap->rmdir_recursive($dn);
379     }
381     /* move follwoing entries 
382      */
383     foreach($tmp['move'] as $src => $dst){
384       $this->recursive_move($src,$dst);
385     }
387     /* Add || Update new DNS entries 
388      */
389     foreach($tmp['add'] as $dn => $attrs){
390       $ldap->cd($dn);
391       $ldap->cat($dn);
392       if(count($ldap->fetch())){
393         $ldap->cd($dn);
394         $ldap->modify ($attrs); 
395       }else{
396         $ldap->cd($dn);
397         $ldap->add($attrs);
398       }
399     }
400     
401     /* Display errors 
402      */
403     if($ldap->get_error() != "Success"){
404       show_ldap_error("Record:".$ldap->get_error()); 
405     }
406   }
408   /*  Create html table with all used record types
409    */
410   function generateRecordsList()
411   {
412     $changeStateForRecords = "";
413     
414     if(!$this->DNS_is_account) {
415       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
416       return $str;
417     }
418  
419     $str = "<table summary='' width='100%'>";
420     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
422         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
423         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
424         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
426         $str.=" <tr>".
427           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
428           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
429           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
430           "</tr>";
431     }
433     $str.= "  <tr>".
434            "    <td colspan=2 width='50%'></td><td>".
435            "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
436            "    </td>".
437            "  </tr>".
438            "</table>";
439      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
440     return($ret);
441   }
444   /* Create a html select box which allows us to select different types of records 
445    */
446   function generateRecordListBox($selected,$name)
447   {
448     $str = "<select name='".$name."' id='".$name."'>";
449     foreach($this->RecordTypes as $type => $value){
450       $use = "";
451       if($type == $selected){
452         $use = " selected ";
453       }
454       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
455     }
456     $str.="</select>";
457     return($str); 
458   }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>