Code

Removed labeledUri = labeledURI fix
[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;
12   var $DNSattributes   = array("dNSClass","zoneName","dNSTTL");
13   var $attributes= array("ipHostNumber","macAddress");
14   var $objectclasses= array("whatever");
16   var $ipHostNumber ="";    // IP address 
17   var $macAddress   ="";    // Mac address 
18   var $cn           ="";    // CN of currently edited device 
20   var $Zones      = array();      // All Available Zones like array("3.2.1"=>"MyServer.de")
21   var $RecordTypes= array();      // Possible record types 
22     
23   var $dNSClass   = "IN";         // dNSClass name 
24   var $zoneName   = "";           // Used ZoneName 
25   var $dNSTTL     = 7200;         // TTL settings for the created entries 
27   /* Used records */
28   var $types = array();
29   var $DNSinitially_was_account = false;
31   var $orig_dn     ="";
33   var $IPisMust = false;
34   var $MACisMust= false;
36   function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
37   {
38     /* We need to know which objectClasses are used, to store the ip/mac*/
39     $this->objectclasses= $objectClasses;
40     plugin::plugin ($config, $dn);
42     $this->orig_dn= $dn;
43  
44     $this->IPisMust = $IPisMust;
45  
46     /* All types with required attrs */
47     $this->RecordTypes['aRecord']       = "aRecord";           // ok
48     $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
49     $this->RecordTypes['mXRecord']      = "mXRecord";          // ok
50     $this->RecordTypes['nSRecord']      = "nSRecord";          // ok 
51     $this->RecordTypes['pTRRecord']     = "relativeDomainName";// ok
52     $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
53     $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
54     $this->RecordTypes['cNAMERecord']   = "relativeDomainName";// ok
55     $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
56     $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
57     $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
58     $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok 
59     $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
60     $this->RecordTypes['LocRecord']     = "LocRecord";         // ok 
61     $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
62     $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
63     $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
64     $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
65     $this->RecordTypes['certRecord']    = "certRecord";        // ok
66     $this->RecordTypes['a6Record']      = "a6Record";          // ok
67     $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
68     $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
69     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
70     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok 
72     echo $this->cn;
73     /* Get all available zones */
74     if(empty($this->cn)&&(isset($this->attrs['cn'][0]))){ 
75       $this->cn = $this->attrs['cn'][0];
76     }
77     $this->Zones = $this->get_Zones();
78     $types = array();
80     /* Get all records */
81     $ldap = $this->config->get_ldap_link();
82     $ldap->cd($this->dn); 
83     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("*"));
85     while($attrs = $ldap->fetch()){
86       /* If relative domainname == cn 
87        * Try to read dnsclass / TTl / zone 
88        */
89       if($attrs['relativeDomainName'][0] == $this->cn){
90         /* Get class */
91         if(isset($attrs['dNSClass'][0])){
92           $this->dNSClass = $attrs['dNSClass'][0];
93         }
94         /* Get Zone*/
95         if(isset($attrs['zoneName'][0])){
96           $this->zoneName = $attrs['zoneName'][0];
97         }
98         /* Get ttl */
99         if(isset($attrs['dNSTTL'][0])){
100           $this->dNSTTL   = $attrs['dNSTTL'][0];
101         }
102       }
104       /* Create list with all used records */
105       foreach($this->RecordTypes as $name => $value){
106     
107         /* If there is a record attribute  */
108         if(isset($attrs[$name])){
109   
110  
111           /* get all entries */
112           for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
113             if(($value == "aRecord")&&($this->ipHostNumber==$attrs[$value][$i])){
114              continue; 
115             }
116             $types[] =array("type"=>$name,"inittype"=>$name,"value"=>$attrs[$value][$i],"status"=>"edited","dn"=>$attrs['dn']);
117           }
118         }
119       }
120     }
122     /* If there is at least one entry in this -> types, we have DNS enabled */
123     $this->types = $types;
124     if(count($this->types) == 0){
125       $this->DNS_is_account = false;
126     }else{
127       $this->DNS_is_account = true;
128     }
129     
130     /* Store initally account settings */
131     $this->DNSinitially_was_account = $this->DNS_is_account;
132   }
134   function execute()
135   {
136           /* Call parent execute */
137     $smarty= get_smarty();
138     $display= "";
140     /* Add new empty array with status new, to our record list */
141     if(isset($_POST['AddNewRecord'])){
142       $this->types[]  =array("type"=>"aRecord","value"=>"","status"=>"new");
143     }
144    
145     /* Handle all posts */ 
146     $only_once =true;
147     foreach($_POST as $name => $value){
149       /* Check if we have to delete a record entry */
150       if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
151       
152         /* Avoid performing this once again */
153         $only_once = false;
155         /* Extract id for specified entry */
156         $id = preg_replace("/RemoveRecord_/","",$name);
157         $id = preg_replace("/_.*$/","",$id);
158     
159         /* Delete this record, mark edited entries to be able to delete them */
160         if(isset($this->types[$id])){
161           if($this->types[$id]['status'] == "edited"){
162             $this->types[$id]['status'] = "deleted";
163           }else{    
164             unset($this->types[$id]);
165           }
166         }
167       }
168     }
170     /* Assign smarty all non DNs attributes */
171     foreach($this->attributes as $attr){
172       $smarty->assign($attr,$this->$attr);
173     }
175     /* Assign smarty all DNS attributes */
176     foreach($this->DNSattributes as $attr){
177       $smarty->assign($attr,$this->$attr);
178     }
179     
180     /* Assign all needed vars */
181     $smarty->assign("DNSAccount",$this->DNS_is_account);
182     $smarty->assign("Zones",$this->Zones);
183     $smarty->assign("ZoneKeys",($this->Zones));
184     $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
185     $changeStateForRecords ="";
186     $smarty->assign("records",$this->generateRecordsList(&$changeStateForRecords));
187     $smarty->assign("changeStateForRecords",$changeStateForRecords);
188 //    $smarty->assign("dNSClasses",array("IN"=>"IN"));
189     $smarty->assign("staticAddress","<font class=\"must\">*</font>");
190     $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
191     return($display);
192   }
194   function remove_from_parent()
195   {
196     $ldap = $this->config->get_ldap_link();
197     $ldap->cd($this->orig_dn);
198     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
199     while($attr = $ldap->fetch()){  
200       $ldap->cd($attr['dn']);
201       $ldap->rmDir($attr['dn']);
202     }
203   }
205   /* Save data to object */
206   function save_object()
207   {
208     /* Save all posted vars */
209     plugin::save_object();
210     
211     /* Ge all non dns attributes (IP/MAC)*/
212     foreach($this->attributes as $attr){
213       if(isset($_POST[$attr])){
214         $this->$attr = $_POST[$attr];
215       }
216     }
218     /* Get dns attributes */
219     if(isset($_POST['network_tpl_posted'])){
221       /* Check for posted record changes */
222       foreach($this->types as $key => $value){
223       
224         /* Check if type has changed */
225         if(isset($_POST['RecordTypeSelectedFor_'.$key])){
226           $this->types[$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
227         }
228         /* Check if value has changed */
229         if(isset($_POST['RecordValue_'.$key])){
230           $this->types[$key]['value'] = $_POST['RecordValue_'.$key];
231         }
232       }
233       
234       /* Get all basic DNS attributes (TTL, Clas ..)*/
235       foreach($this->DNSattributes as $attr){
236         if(isset($_POST[$attr])){
237           $this->$attr = $_POST[$attr];
238         }
239       }
241       /* Enable diable DNS */
242       if(isset($_POST['enableDNS'])){
243         $this->DNS_is_account = true;
244       }else{
245         $this->DNS_is_account = false;
246       }
247     }
248   }
251   /* Check supplied data */
252   function check()
253   {
254     $message= array();
256     
257     if(($this->IPisMust)||($this->DNS_is_account)){
258       /* Check if ip is empty */
259       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
260         $message[]= _("The required field 'IP-address' is not set.");
261       }
263       /* check if given ip is valid ip*/
264       $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
265       if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
266         $message[]= _("Wrong IP format in field IP-address.");
267       }
268     }
270     /* Check if mac is empty */
271     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
272       $message[]= _("The required field 'MAC-address' is not set.");
273     }
275     /* Check if given mac is valid mac */
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){
283       foreach($this->types as $name => $values){
285         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress */
286         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)&&($values['status']!="deleted")){
287           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
288                $this->ipHostNumber);
289         }
291         /* only lower-case is allowed in record entries ... */
292         if($values['value'] != strtolower($values['value'])){
293           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
294         }
295       }
296     }
297   
298     return ($message);
299   }
302   /* Save to LDAP */
303   function save($dn)
304   {
305     $ldap= $this->config->get_ldap_link();
306    
307     /*******************/ 
308     /* IP-MAC HANDLING */
309     /*******************/ 
311     /* $dn was posted as parameter */
312     $this->dn = $dn;
313     
314     /* Save DNS setting & ip/Mac*/
315     plugin::save();
317     /* Write back to ldap */
318     $ldap->cd($this->dn);
319     $ldap->modify($this->attrs);
321     /****************/ 
322     /* DNS HANDLING */
323     /****************/ 
325     /* If isn't DNS account but initially was DNS account 
326        remove all DNS entries 
327      */ 
328     if(!$this->DNS_is_account){
329       if($this->DNSinitially_was_account){
330         $tmp = array();
331         foreach($this->types as $type){
332           $dn = $type['dn'];
333           if(!isset($tmp[$dn])) {
334             $ldap->cd($dn);
335             $ldap->rmDir($dn);
336           }
337           $tmp[$dn]=$dn;
338         }
339       }
340     }else{
341     
342       /* DNS is enabled, check what we have to do */
343       $delete = array();
344   
345       /* Generate a list of new ldap entries, 
346           & $delete contains all dns which should be deleted 
347        */
348       $entries = $this->generate_LDAP_entries(&$delete);
350       /* Delete dns */
351       foreach($delete as $dn => $del){
352         $ldap->cd($dn);
353         $ldap->rmDir($dn);
354       }
356       /* Add || Update new DNS entries */
357       foreach($entries as $dn => $attrs){
358         $ldap->cd($dn);
359         $ldap->cat($dn);
360         
361         if(count($ldap->fetch())){
362           $ldap->cd($dn);
363           $ldap->modify($attrs);
364         }else{
365           $ldap->cd($dn);
366           $ldap->add($attrs);
367         }
368       }
369     }
370     if($ldap->get_error() != "Success"){
371       show_ldap_error($ldap->get_error()); 
372     }
373     
374   }
376   /*  Create html table with all used record types
377    */
378   function generateRecordsList($changeStateForRecords)
379   {
380     $changeStateForRecords = "";
381     
382     if(!$this->DNS_is_account) {
383       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
384       return $str;
385     }
386  
387     $str = "<table summary='' width='100%'>";
388     foreach($this->types as $key => $entry){
389         if($entry['status'] == "deleted") continue;
391         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
392         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
393         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
395         $str.=" <tr>".
396           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
397           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
398           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
399           "</tr>";
400     }
402     $str.= "  <tr>".
403            "    <td colspan=2 width='50%'></td><td>".
404            "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
405            "    </td>".
406            "  </tr>".
407            "</table>";
408     return($str);
409   }
411   /* Create a html select box which allows us to select different types of records */
412   function generateRecordListBox($selected,$name)
413   {
414     $str = "<select name='".$name."' id='".$name."'>";
415     foreach($this->RecordTypes as $type => $value){
416       $use = "";
417       if($type == $selected){
418         $use = " selected ";
419       }
420       $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
421     }
422     $str.="</select>";
423     return($str); 
424   }
426   /* return all Zone names */
427   function get_Zones()
428   {
429     $ret = array();
430     $ldap = $this->config->get_ldap_link();
431     $ldap-> cd ($this->config->current['BASE']);
432     $ldap->search("(&(objectClass=dNSZone)(sOARecord=*))",array("*"));
433     
434     while($at = $ldap->fetch()){
435       if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){
436         $ret[$at['relativeDomainName'][0]]['addr']= $at['zoneName'][0];
437       }else{
438         $ret[$at['relativeDomainName'][0]]['name']= $at['zoneName'][0];
439       }
440     }
442     $tmp  =array();
443     foreach($ret as $name => $entry){
444       if((isset($entry['addr']))&&(isset($entry['name']))){
445         $tmp[$entry['addr']]=$entry['name'];
446       }
447     }
448     $ret = $tmp;
449     return($ret); 
450   } 
452   /* this is used to generate ldap friendly output of our 
453      dns configuration
454   */
455   function generate_LDAP_entries($delete)
456   {
457     $entries = array();    
458     $delete  = array();
460     /* Generate Main Entry */
461     $dn = "relativeDomainName=".$this->cn.",".$this->dn;
462     $entries[$dn]['dNSClass']           = $this->dNSClass;      
463     $entries[$dn]['zoneName']           = $this->zoneName;      
464     $entries[$dn]['dNSTTL']             = $this->dNSTTL;      
465     $entries[$dn]['relativeDomainName'] = $this->cn;      
467     /* Generate cNAMERecord */
468     $aRecords = array();
469     foreach($this->types as $type){
470       if($type['type'] == "cNAMERecord"){
471           
472         $Cdn = "relativeDomainName=".$type['value'].",".$this->dn;
473         if($type['status']=="deleted"){
474           $delete [$type['dn']] = $Cdn;
475         }else{
476           $entries[$Cdn] = $entries[$dn];
477           $entries[$Cdn]['relativeDomainName']  = $type['value'];
478           $entries[$Cdn]['cNAMERecord']         = $this->cn.".".$this->zoneName;
479         }
480       }
481     }
483     /* Generate tXTRecord */
484     $aRecords = array();
485     foreach($this->types as $type){
486       if(($type['type'] == "tXTRecord")&&($type['status']!="deleted")){
487         $entries[$dn]['tXTRecord'][] = $type['value'];
488       }
489     }
491     /* Generate mDRecord */
492     $aRecords = array();
493     foreach($this->types as $type){
494       if(($type['type'] == "mDRecord")&&($type['status']!="deleted")){
495         $entries[$dn]['mDRecord'][] = $type['value'];
496       }
497     }
499     /* Generate mXRecord */
500     $aRecords = array();
501     foreach($this->types as $type){
502       if(($type['type'] == "mXRecord")&&($type['status']!="deleted")){
503         $entries[$dn]['mXRecord'][] = $type['value'];
504       }
505     }
506  
507     /* Generate hInfoRecord */
508     $aRecords = array();
509     foreach($this->types as $type){
510       if(($type['type'] == "hInfoRecord")&&($type['status']!="deleted")){
511         $entries[$dn]['hInfoRecord'][] = $type['value'];
512       }
513     }
515     /* Generate mInfoRecord */
516     $aRecords = array();
517     foreach($this->types as $type){
518       if(($type['type'] == "mInfoRecord")&&($type['status']!="deleted")){
519         $entries[$dn]['mInfoRecord'][] = $type['value'];
520       }
521     }
522  
523     /* Generate aFSDBRecord */
524     $aRecords = array();
525     foreach($this->types as $type){
526       if(($type['type'] == "aFSDBRecord")&&($type['status']!="deleted")){
527         $entries[$dn]['aFSDBRecord'][] = $type['value'];
528       }
529     }
530  
531     /* Generate some attrs  */
532     $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord",
533         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
534     $aRecords = array();
535     foreach($arr as $ar){
536       foreach($this->types as $type){
537         if(($type['type'] == $ar)&&($type['status']!="deleted")){
538           $entries[$dn][$ar][] = $type['value'];
539         }
540       }
541     }
542  
543  
544     /* Generate A Records (IP Address relation) */
545     $aRecords = array();
546     foreach($this->types as $type){
547       if(($type['type'] == "aRecord")&&($type['status']!="deleted")){
548         $aRecords[] = $type['value'];
549       }
550     }
551     if(count($aRecords)){
552       
553       /* Add ipHostNumber as default aRecord */
554       $aRecords[] = $this->ipHostNumber;
556       $dn = "relativeDomainName=".$this->cn.",".$this->dn;
557       foreach($aRecords as $rec){
558         $entries[$dn]['aRecord'][] = $rec;      
559       }
560     }
562     /* Generate pTRRecord Records */
563     foreach($this->types as $type){
564       if($type['type'] == "pTRRecord"){
565         $PTRdn= "relativeDomainName=".$type['value'].",".$this->dn;
566         if($type['status']=="deleted"){        
567           $delete [$type['dn']] = $PTRdn;
568         }else{
569           $zones = array_flip($this->Zones);
570           $zone = $zones[$this->zoneName];  
571           $entries[$PTRdn]['relativeDomainName'] = $type['value'];
572           $entries[$PTRdn]['pTRRecord']         = $this->cn.".".$this->zoneName;
573           $entries[$PTRdn]['zoneName']            =  $zone;
574         }
575       }
576     }
578     /* add ObjectClasses */
579     foreach($entries as $key => $entry ){
580       $entries[$key]['objectClass']=array("top","dNSZone");
581       $entries[$key] = array_reverse($entries[$key]);
582     }
584     /* Check if record type has changed, and if we need to delete this record attribute from ldap entry */
585     foreach($this->types as $type){
586       if(isset($type['inittype'])){
587         if(!isset($entries[$dn][$type['inittype']])){
588           $entries[$dn][$type['inittype']] = array();
589         }
590       }
591     }
593     return($entries); 
594   }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>