Code

Removed labeledUri = labeledURI fix
[gosa.git] / plugins / admin / systems / class_servDNS.inc
index 37471e7cc92c70795c666c40d7f6b277b98d9138..13c2956007036ade69e71bf3cef73de14f1d76d7 100644 (file)
@@ -8,7 +8,7 @@ class servdns extends plugin
   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
 
   /* attribute list for save action */
-  var $ignore_account= TRUE;
+  var $ignore_account   = FALSE;
   var $attributes       = array(""); 
   var $objectclasses    = array("whatever");
 
@@ -18,10 +18,18 @@ class servdns extends plugin
   var $dialog = NULL;
 
   var $usedDNS    = array();
+
+  var $orig_dn = "";
+
+  var $DNSinitially_was_account;
+
+
   function servdns ($config, $dn= NULL)
   {
     plugin::plugin ($config, $dn);
 
+    $this->orig_dn = $dn;
+
     /* All types with required attrs */
     $this->RecordTypes['aRecord']       = "aRecord";           // ok
     $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
@@ -47,8 +55,6 @@ class servdns extends plugin
     $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
     $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
 
-    $this->cn = $this->attrs['cn'][0];
-
     $types = array();
 
     /* Get all records */
@@ -57,13 +63,17 @@ class servdns extends plugin
     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
 
     while($attrs = $ldap->fetch()){
-      /* If relative domainname == cn
+      /* If relative domainname 
        * Try to read dnsclass / TTl / zone
        */
-      $this->usedDNS[$attrs['dn']] = $attrs;
+      $this->usedDNS[$attrs['dn']] = $attrs['dn'];
       if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
         $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
-        $types[$zoneName]['ReverseZone']  = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
+        $z = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
+        
+        $z = $this->FlipIp($z);
+
+        $types[$zoneName]['ReverseZone']  = $z;
         $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
       }else{
 
@@ -98,17 +108,14 @@ class servdns extends plugin
           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
         }
 
-        /* Set status */
-//        $types[$attrs['zoneName'][0]]['status']   ="edited"; 
-//        $types[$attrs['zoneName'][0]]['Reversestatus']   ="edited"; 
-//        $types[$attrs['zoneName'][0]]['ReverseDN']= $attrs['dn']; 
-
         /* Create list with all used records */
         foreach($this->RecordTypes as $name => $value){
 
           /* If there is a record attribute  */
           if(isset($attrs[$name])){
 
+            $types[$attrs['zoneName'][0]]['Records']=array();
+
             /* get all entries */
             for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
               $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
@@ -134,6 +141,22 @@ class servdns extends plugin
     $this->DNSinitially_was_account = $this->is_account;
   }
 
+
+  /* this is used to flip the ip address for example 
+      12.3.45  ->  54.3.12     
+     Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
+      but we want to display 12.3.45.
+  */
+  function FlipIp($ip)
+  {
+    $tmp = array_reverse(split("\.",$ip));
+    $new = "";
+    foreach($tmp as $section){
+      $new .= $section.".";
+    }
+    return(preg_replace("/.$/","",$new));
+  }
+
   function execute()
   {
     /* Call parent execute */
@@ -158,79 +181,77 @@ class servdns extends plugin
       return ($display);
     }
 
+    /* Edited or Added zone hould be saved saved */
     if(isset($_POST['SaveZoneChanges'])){
       $this->dialog->save_object();
-    
+
+      /* Check if noting went wrong */
       if(count($this->dialog->check())){
         foreach($this->dialog->check() as $msgs){
           print_red($msgs); 
         }
       }else{
-
+      
+        /* add new/edited zone */
         $ret = $this->dialog->save();
         unset($this->Zones[$this->dialog->InitiallyZoneName]);
         $this->Zones[$ret['zoneName']]                  = $ret;
-
-        /*
-        if($this->dialog->isNew == true){
-          $this->Zones[$ret['zoneName']] = $ret;
-          $this->Zones[$ret['zoneName']] = "new";
-        }else{
-
-          if(!$this->dialog->isNew){
-    
-            $ForStatus = $this->Zones[$this->dialog->InitiallyZoneName]['status'];
-            $RevStatus = $this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'];
-  
-            if($ret['zoneName'] != $this->dialog->InitiallyZoneName){
-              if($this->Zones[$this->dialog->InitiallyZoneName]['status'] != "new"){
-                $this->Zones[$this->dialog->InitiallyZoneName]['status'] = "deleted";
-              }else{
-                unset($this->Zones[$this->dialog->InitiallyZoneName]);
-              }
-              $this->Zones[$ret['zoneName']]                  = $ret;
-              $this->Zones[$ret['zoneName']]['status']        = "new";
-              $this->Zones[$ret['zoneName']]['Reversestatus'] = $RevStatus;
-
-            }elseif($ret['ReverseZone'] != $this->dialog->InitiallyReverseZone){
-              if($this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'] != "new"){
-                $this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'] = "deleted";
-              }else{
-                unset($this->Zones[$this->dialog->InitiallyZoneName]);
-              }
-              $this->Zones[$ret['zoneName']]                  = $ret;
-              $this->Zones[$ret['zoneName']]['Reversestatus'] = "new";
-              $this->Zones[$ret['zoneName']]['status']        = $ForStatus;
-
-            }else{
-              $this->Zones[$ret['zoneName']]                  = $ret;
-              $this->Zones[$ret['zoneName']]['status']        = $ForStatus;
-              $this->Zones[$ret['zoneName']]['Reversestatus'] = $RevStatus;
-            }
-          }
-        }
-      */
         $this->dialog = NULL;
       }
     }
+
+    /* Cancel zone edit / new */
     if(isset($_POST['CancelZoneChanges'])){
       $this->dialog = NULL;
     }
+
     /* Add empty new zone */
     if(isset($_POST['AddZone'])){
       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
     }
 
+    /* Check for edit zone request */
     $once = false;
     foreach( $_POST as $name => $value){
+  
+      /* check all post for edit request */
       if(preg_match("/^editZone_/",$name)&&!$once){
         $once =true;
         $tmp = preg_replace("/^editZone_/","",$name);
         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
       }
+
+      /* check posts for delete zone */
+      if(preg_match("/^delZone_/",$name)&&!$once){
+
+
+        $once =true;
+        $tmp = preg_replace("/^delZone_/","",$name);
+        $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
+  
+        $zones =  $this->getUsedZoneNames();
+        $rev = $this->Zones[$tmp]['ReverseZone'];
+        $res = array_merge(($zones[$tmp]),($zones[$rev.".in-addr.arpa"]));
+        
+        if(count($res)){
+          $i = 2;
+          $str ="";
+          foreach($res as $dn){
+            if($i > 0 ){
+              $i --;
+              $str.=$dn." ";
+            }
+          }
+          if(count($res)> 2) $str .=" ... ";
+          print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
+        }else{
+          unset($this->Zones[$tmp]);
+        }
+      }
     }
 
+    /* Show dialog */
     if($this->dialog!= NULL){
       $this->dialog->save_object();
       $this->dialog->parent = $this;
@@ -242,15 +263,16 @@ class servdns extends plugin
     $ZoneList -> SetHeight(254);
 
     /* Add entries to divlist*/
-    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
+    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
+      <input type='image' src='images/edittrash.png' name='delZone_%s'>";
     foreach($this->Zones as $zone => $values ){
       $ZoneList->AddEntry(array(
-                                array("string" => $zone),
-                                array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
-                                array("string" => _("TTL")." : ".$values['dNSTTL']),
-                                array("string" => _("Class")." : ".$values['dNSClass']),
-                                array("string" =>str_replace("%s",base64_encode($zone),$editImg))
-                          ));
+            array("string" => $zone),
+            array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
+            array("string" => _("TTL")." : ".$values['dNSTTL']),
+            array("string" => _("Class")." : ".$values['dNSClass']),
+            array("string" =>str_replace("%s",base64_encode($zone),$editImg))
+            ));
     }    
 
     /* Display tempalte */
@@ -259,8 +281,49 @@ class servdns extends plugin
     return($display);
   }
 
+
+  /* This funtion returns all used Zonenames */
+  function getUsedZoneNames()
+  {
+    $ret = array();
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName","tXTRecord"));
+    while($attr = $ldap->fetch()){
+      if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
+        $ret[$attr['zoneName'][0]][] = $attr['dn'];
+      }else{
+        $ret[$attr['zoneName'][0]][] = $attr['dn'];
+      }
+    }
+    return($ret);
+  }
+
+
+  /* Remove dns service */
   function remove_from_parent()
   {
+    if(!$this->DNSinitially_was_account){
+      return;
+    }
+
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    foreach($this->usedDNS as $dn){
+      $ldap->cd($dn);
+      $ldap->rmdir_recursive($dn);
+    }
+
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->orig_dn);
+    $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",array("relativeDomainName","zoneName"));
+    while($attr = $ldap->fetch()){
+      $ldap->cd($attr['dn']);
+      $ldap->rmDir($attr['dn']);
+    }
+
+
+    show_ldap_error($ldap->get_error());
   }
 
 
@@ -281,13 +344,18 @@ class servdns extends plugin
   /* Save to LDAP */
   function save()
   {
+    /* Ldap conenction / var initialization */
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);
-
     $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
 
+    /* Generate entries for all zones, and check if they must be updated deleted added */
     foreach($this->Zones as $zone){
+      
+      /* Get ldap syntax */
       $tmp = $this->generate_LDAP_entries($zone);
+
+      /* Check if dn is new, or if entry was edited */
       foreach($tmp as $key => $values){
         if(isset($this->usedDNS[$key])){
           $actions['update'][$key]=$values;
@@ -297,22 +365,27 @@ class servdns extends plugin
         }
       }
     }
+    
+    /* Check which dns are not used anymore ...*/
     foreach($this->usedDNS as $key => $values){
       $actions['delete'][$key] = $values;
     }
 
+    /* Remove deleted zones */
     foreach($actions['delete'] as $dn => $attrs){
       $ldap->cd($dn);
-      $ldap->rmDir($dn);
+      $ldap->rmdir_recursive($dn);
     }
 
+    /* Add new zones */
     foreach($actions['add'] as $dn => $attrs){
       $ldap->cd($this->config->current['BASE']);
-//      $ldap->create_missing_trees($dn);
+      //      $ldap->create_missing_trees($dn);
       $ldap->cd($dn);
       $ldap->add($attrs);
     }
 
+    /* Update existing entries */
     foreach($actions['update'] as $dn => $attrs){
       $ldap->cd($dn);
       $ldap->modify($attrs);
@@ -321,6 +394,9 @@ class servdns extends plugin
   }
 
 
+  /* This function generates ldap friendly output 
+     of all changes for a single zone (reverse and forward)
+   */
   function generate_LDAP_entries($zone)
   {
     $tmp = array();
@@ -336,33 +412,44 @@ class servdns extends plugin
     $tmp['sOARecord'] = $str;
 
 
-    /* Generate some attrs  */
+    /* Generate Record entries  */
     $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
     $aRecords = array();
     foreach($arr as $ar){
-      foreach($zone['Records'] as $type){
-        if(($type['type'] == $ar)&&($type['status']!="deleted")){
-          $tmp[$ar][] = $type['value'];
+      if((isset($zone['Records']))&&(is_array($zone['Records']))){
+        foreach($zone['Records'] as $type){
+          if(($type['type'] == $ar)&&($type['status']!="deleted")){
+            $tmp[$ar][] = $type['value'];
+          }
         }
       }
     }
-    if(is_array($zone['Records'])){
+   
+    /* Check if there are records removed,
+        if there are some removed records, the append an array        
+        to ensure that these record types are deleted 
+     */
+    if((isset($zone['Records']))&&(is_array($zone['Records']))){
       foreach($zone['Records'] as $type){
-        if(isset($type['inittype'])){
+        if((isset($type['inittype']))&&($type['inittype']!="")){
           if($type['type'] != $type['inittype']){
             $tmp[$type['inittype']] = array();
           }
         }
       }
     }
+    
+    /* generate forward entry */
     $dn = "zoneName=".$zone['zoneName'].",".$this->dn; 
     $tmp2[$dn] = $tmp;
     $tmp2[$dn]['zoneName'] = $zone['zoneName'];
-    $dn = "zoneName=".$zone['ReverseZone'].".in-addr.arpa,".$this->dn;
+
+    /* generate reverse entry */
+    $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
     $tmp2[$dn] = $tmp;
     $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
-    $tmp2[$dn]['zoneName'] = $zone['ReverseZone'].".in-addr.arpa";
+    $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
 
     return($tmp2);
   }