Code

Updated workstation - Startup
[gosa.git] / plugins / admin / systems / class_servDNS.inc
index e532daba3881fabe1218a8364f34dc4bcb11e000..fed9c92f71120d54fd01530aac9e7d3fe50f5c8c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class servdns extends plugin
+class servdns extends goService
 {
   /* CLI vars */
   var $cli_summary= "Manage server basic objects";
@@ -14,7 +14,6 @@ class servdns extends plugin
 
   var $RecordTypes      = array();
   var $Zones            = array();
-  var $dialog           = NULL;
 
   var $orig_dn          = "";
 
@@ -24,12 +23,17 @@ class servdns extends plugin
   var $conflicts        = array("servdns");
   var $DisplayName      = "";
   var $StatusFlag       = "";
+  var $view_logged      = FALSE;
 
-  function servdns ($config, $dn= NULL)
+  var $dns_server_list   = array("ENTRIES"=> array(),"FOR_LIST"=> array());
+  var $take_over_id       = -1;
+
+
+  function servdns (&$config, $dn= NULL, $parent= NULL)
   {
-    plugin::plugin ($config, $dn);
+    plugin::plugin ($config, $dn, $parent);
 
-    $this->DisplayName = _("Domain name system service");
+    $this->DisplayName = _("DNS service");
 
     $this->orig_dn = $dn;
 
@@ -45,6 +49,7 @@ class servdns extends plugin
      */
     if(count($this->Zones) == 0){
       $this->is_account = false;
+      $this->dns_server_list = $this->get_list_of_dns_servers();
     }else{
       $this->is_account = true;
     }
@@ -52,17 +57,110 @@ class servdns extends plugin
   }
 
 
+  function get_list_of_dns_servers()
+  {
+    $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=dNSZone)(zoneName=*))",array("dn","zoneName"));
+    $dns = array();
+    while($attrs = $ldap->fetch()){
+      /* Skip own config */
+      if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
+        continue;
+      }
+      $dn = preg_replace("/^zoneName=[^,]+,/","",$attrs['dn']);
+      if(preg_match("/^cn=/",$dn) && !in_array($dn,$dns)){
+        $dns[] = $dn;
+      }
+    }
+    $i = 0;
+    foreach($dns as $dn){
+      $ldap->cat($dn,array('*'));
+      if($ldap->count()){
+        $i ++;
+        $attrs = $ldap->fetch();
+          $ret['ENTRIES'][$i]   = $attrs;
+          $ret['FOR_LIST'][$i] = $attrs['cn'][0];
+      }
+    }
+    return($ret);
+  }  
+
+
+  function get_dns_info_string($id)
+  {
+    $ret="";
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->dns_server_list['ENTRIES'][$id]['dn']);
+    $ldap->search("(|(zoneName=*)(relativeDomainName=*))",array("dn"));
+    while($attrs = $ldap->fetch()){
+      $ret .= $attrs['dn']."\n";
+    }
+    return($ret);
+  }
+
+
   function execute()
   {
     /* Call parent execute 
      */
     plugin::execute();
 
+    if($this->is_account && !$this->view_logged){
+      $this->view_logged = TRUE;
+      new log("view","server/".get_class($this),$this->dn);
+    }
+
     /* Fill templating stuff 
      */
     $smarty= get_smarty();
+    $smarty->assign("dns_take_over",FALSE);
+    $smarty->assign("is_createable",$this->acl_is_createable());
     $display= "";
 
+
+    $this->initially_was_account= $this->is_account;
+    /*****************/
+    /* Handle Take Over Actions
+    /*****************/
+
+    /* Give smarty the required informations */
+    $smarty->assign("dns_server_list", $this->dns_server_list['FOR_LIST']);
+    $smarty->assign("dns_server_list_cnt", count($this->dns_server_list['FOR_LIST']));
+
+    /* Take over requested, save id */
+    if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
+      $id = $_POST['take_over_src'];
+      if(isset($this->dns_server_list['ENTRIES'][$id])){
+        $this->take_over_id = $id;
+      }
+    }
+
+    /* Abort take over action */
+    if(isset($_POST['cancel_take_over'])){
+      $this->dialog =false;
+      $this->take_over_id = -1;
+      $this->dns_server_list = $this->get_list_of_dns_servers();
+    }
+
+    /* Display informartion about take over that will be started when saving this server
+     *  and hide default dns output
+     */
+    if($this->take_over_id != -1){
+      $this->dialog = FALSE;
+      $id = $this->take_over_id;
+      $info = $this->get_dns_info_string($id);
+      $smarty->assign("dns_take_over",TRUE);
+      $smarty->assign("info",$info);
+      $warning = sprintf(_("You are going to migrate the DNS setup from server '%s'."),$this->dns_server_list['ENTRIES'][$id]['cn'][0]);
+      $warning2 = _("The migration will be startet when you save this system. To cancel this action, use the cancel button below.");
+      $smarty->assign("warning",$warning);
+      $smarty->assign("warning2",$warning2);
+      return($smarty->fetch(get_template_path('servdns.tpl', TRUE)));
+    }
+
+
     /* Do we need to flip is_account state? 
      */
     if (isset($_POST['modify_state'])){
@@ -70,10 +168,10 @@ class servdns extends plugin
     }
 
     if ($this->is_account){
-      $display= $this->show_header(_("Remove DNS service"),
+      $display= $this->show_disable_header(_("Remove DNS service"),
           _("This server has DNS features enabled. You can disable them by clicking below."));
     } else {
-      $display= $this->show_header(_("Add DNS service"),
+      $display= $this->show_enable_header(_("Add DNS service"),
           _("This server has DNS features disabled. You can enable them by clicking below."));
       return ($display);
     }
@@ -98,14 +196,14 @@ class servdns extends plugin
           unset($this->Zones[$this->dialog->OldZoneName]);
         }
         $this->Zones[$ret['zoneName']] = $ret;
-        $this->dialog = NULL;
+        $this->dialog = FALSE;
       }
     }
 
     /* Cancel zone edit / new 
      */
     if(isset($_POST['CancelZoneChanges'])){
-      $this->dialog = NULL;
+      $this->dialog = FALSE;
     }
 
     /* Add empty new zone 
@@ -142,9 +240,23 @@ class servdns extends plugin
       }
     }
 
+    if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
+      $id = base64_decode($_GET['id']);
+      if(isset($this->Zones[$id])){
+         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
+      }
+    }
+
+    if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
+      $id = base64_decode($_GET['id']);
+      if(isset($this->Zones[$id])){
+         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
+      }
+    }
+
     /* Show dialog 
      */
-    if($this->dialog!= NULL){
+    if(is_object($this->dialog)){
       $this->dialog->save_object();
       $this->dialog->parent = $this;
       return($this->dialog->execute());
@@ -157,12 +269,16 @@ class servdns extends plugin
 
     /* Add entries to divlist
      */
-    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
-      <input type='image' src='images/edittrash.png' name='delZone_%s'>";
+    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
+    if($this->acl_is_removeable()){
+      $editImg.= "<input type='image' src='images/edittrash.png' name='delZone_%s'>";
+    }
+
+    $link = "<a href='?plug=".$_GET['plug']."&act=edit&id=%s'>%s</a>";
     foreach($this->Zones as $zone => $values ){
       $ZoneList->AddEntry(array(
-            array("string" => $zone),
-            array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
+            array("string" => sprintf($link,base64_encode($zone),($zone))),
+            array("string" => sprintf($link,base64_encode($zone),_("Reverse zone")." : ".($values['ReverseZone']))),
             array("string" => _("TTL")." : ".$values['sOAttl']),
             array("string" => _("Class")." : ".$values['dNSClass']),
             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
@@ -210,14 +326,16 @@ class servdns extends plugin
 
     /* There are still entries using this configuration
      *  Abort deletion
-     */ 
+     */
     if(count($used)){
       $i = 2;
       $str ="";
       foreach($used as $dn){
-        if($i > 0 ){
+        if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
           $i --;
-          $str.=$dn." ";
+          $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
+          $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
+          $str.= $name.".".$zone." ";
         }
       }
 
@@ -256,6 +374,7 @@ class servdns extends plugin
   {
     if($this->initially_was_account){
       $bool = true;
+      $this->is_account = FALSE;
       foreach($this->Zones as $key => $zone){
         $bool= $bool & $this->RemoveZone($key);
       }
@@ -271,12 +390,63 @@ class servdns extends plugin
   /* Save to LDAP */
   function save()
   {
+
+    /* Take over handling
+     * - Create list of zones managed by source server 
+     * - Copy ldap entries to destination server 
+     * - Remove old zone entries from source
+     */
+    if($this->take_over_id != -1){
+      $del = array();
+      $id = $this->take_over_id;
+      $src = $this->dns_server_list['ENTRIES'][$id]['dn'];
+      $ldap = $this->config->get_ldap_link(); 
+      $ldap->ls("(objectClass=dnsZone)",$src,array('cn'));
+      while($attrs = $ldap->fetch()){
+        $src_zone = $attrs['dn'];
+        $dst_zone = preg_replace("/".normalizePreg($src)."$/",$this->dn,$src_zone);
+        $res = plugin::recursive_move($src_zone, $dst_zone);
+
+        if($res){
+          $del [] = $src_zone;
+        }
+      }
+      foreach($del as $src_zone){
+        $ldap->rmdir_recursive($src_zone);
+      }
+      return;
+    }
+
+    /* Save zone editor changes now */
+    foreach($this->Zones as $name => $zone){
+      if(isset($zone['zoneEditor'] ) && $zone['zoneEditor'] != NULL && is_object($zone['zoneEditor'])){
+        $zone['zoneEditor']->save();
+        unset($this->Zones[$name]['zoneEditor']);;
+      }
+    }
+
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);  
   
     /* Get differences 
      */
-    $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
+    $old_dn = $this->orig_dn;
+    if($old_dn == "new"){
+      $old_dn = $this->dn;
+    }
+
+    $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
+
+    /* Update dns to current object dn */ 
+    $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
+    $tmp2 = array();
+    foreach($tmp as $key1 => $data1){
+      $tmp2[$key1] = array();
+      foreach($data1 as $key2 => $data2){
+        $tmp2[$key1][preg_replace("/".normalizePreg($old_dn)."$/",$this->dn,$key2)] = $data2;
+      }
+    }
+    $tmp = $tmp2;
 
     /* Updated zone entries if reverser or forward name has changed  
      * Must be done before moving entries, else the given dn is invalid
@@ -285,16 +455,28 @@ class servdns extends plugin
       foreach($tmp['zoneUpdates'] as $dn => $attrs){
         $ldap->cd($dn);
         $ldap->modify($attrs);
-        show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
+        new log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+        show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
       }
     }
 
     /* Delete dns 
      */
     foreach($tmp['del'] as $dn => $del){
+
+      $for = $del['InitialzoneName'];
+      $rev = FlipIp($del['InitialReverseZone']).".in-addr.arpa";
+
       $ldap->cd($dn);
       $ldap->rmdir_recursive($dn);
-      show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
+      new log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+      show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
+
+      /* Handle Post events */
+      if(preg_match("/^zoneName=/",$dn)){
+#        $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $for));
+#        $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $rev));
+      }
     }
 
     /* move follwoing entries
@@ -308,15 +490,27 @@ class servdns extends plugin
     foreach($tmp['add'] as $dn => $attrs){
       $ldap->cd($dn);
       $ldap->cat($dn, array('dn'));
-      if(count($ldap->fetch())){
+      if($ldap->fetch()){
         $ldap->cd($dn);
         $ldap->modify ($attrs);
+        show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
+
+        /* Handle Post events */
+        if(preg_match("/^zoneName=/",$dn)){
+#          $this->handle_post_events("modify",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
+        }
       }else{
         $ldap->cd($dn);
         $ldap->add($attrs);
+        show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
+
+        /* Handle Post events */
+        if(preg_match("/^zoneName=/",$dn)){
+#          $this->handle_post_events("add",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
+        }
       }
-      show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
     }
+    $this->handle_post_events("modify");
   }
 
 
@@ -339,7 +533,7 @@ class servdns extends plugin
       $attrs[$flag] = $value;
       $this->$flag = $value;
       $ldap->modify($attrs);
-      show_ldap_error($ldap->get_error());
+      show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
       $this->action_hook();
     }
   }
@@ -347,18 +541,8 @@ class servdns extends plugin
 
   function getListEntry()
   {
-    $this->updateStatusState();
-    $flag = $this->StatusFlag;
-    if(empty($flag)){
-      $fields['Status']      = "";
-    }else{
-      $fields['Status']      = $this->$flag;
-    }
+    $fields               = goService::getListEntry(); 
     $fields['Message']    = _("DNS service");
-    $fields['AllowStart'] = true;
-    $fields['AllowStop']  = true;
-    $fields['AllowRestart'] = true;
-    $fields['AllowRemove']= true;
     $fields['AllowEdit']  = true;
     return($fields);
   }
@@ -381,6 +565,34 @@ class servdns extends plugin
       $this->$flag = $attrs[$flag][0];
     }
   }
+
+
+  /* Return plugin informations for acl handling */
+  function plInfo()
+  {
+    return (array(
+          "plShortName"   => _("DNS service"),
+          "plDescription" => _("DNS service")." ("._("Services").")",
+          "plSelfModify"  => FALSE,
+          "plDepends"     => array(),
+          "plPriority"    => 83,
+          "plSection"     => array("administration"),
+          "plCategory"    => array("server"),
+
+          "plProvidedAcls"=> array(
+            "zoneName"      =>_("Zone name"),
+            "ReverseZone"   =>_("Reverse zone"),
+            "sOAprimary"    =>_("Primary dns server"),
+            "sOAmail"       =>_("Mail address"),
+            "sOAserial"     =>_("Serial"),
+            "sOArefresh"    =>_("Refresh"),
+            "sOAretry"      =>_("Retry"),
+            "sOAexpire"     =>_("Expire"),
+            "sOAttl"        =>_("TTL"),
+            "zoneRecords"   =>_("Zone records"))
+    ));
+  }
+
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>