Code

Added "Force localboot"
[gosa.git] / plugins / admin / systems / class_termDNS.inc
index d282fe08a0aab6e92258bd20643e4e252ca97b9a..cb678cbccdff2900cf757a87e80b5298bd66a738 100644 (file)
@@ -2,13 +2,9 @@
 
 class termDNS extends plugin
 {
-  /* CLI vars */
-  var $cli_summary      = "Manage server basic objects";
-  var $cli_description  = "Some longer text\nfor help";
-  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 = true;
+  var $autonet        = false;
 
   /* Basic informations 
    */
@@ -25,8 +21,8 @@ class termDNS extends plugin
   /* DNS attributes  
    */
   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
-  var $DNS_is_Account           = false;
-  var $DNSinitially_was_account = false;
+  var $DNS_is_account           = false;
+  var $initially_was_account = false;
   var $dnsEntry                 = array();
   var $DNSenabled               = false;
 
@@ -43,13 +39,19 @@ class termDNS extends plugin
     plugin::plugin ($config, $dn);
 
     if(isset($this->attrs['cn'][0])){
-      $this->OrigCn = $this->attrs['cn'][0];
+      $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
+      $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
+    }
+  
+    /* Do we have autonet support? */
+    if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
+      $this->autonet= true;
     }
 
     /* Hide all dns specific code, if dns is not available 
      */
     $DNSenabled = false;
-    foreach($this->config->data['TABS']['SERVTABS'] as $tab){
+    foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
       if(preg_match("/^servdns$/",$tab['CLASS'])){
         $this->DNSenabled = true;
       }
@@ -88,12 +90,22 @@ class termDNS extends plugin
       }else{
         $this->DNS_is_account = false;
       }
-   
     }
  
     /* Store initally account settings 
      */
-    $this->DNSinitially_was_account = $this->DNS_is_account;
+    $this->initially_was_account = $this->DNS_is_account;
+  }
+
+
+  function netmaskIsCoherent($idZone) 
+  {
+    $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
+    if(!strstr($this->ipHostNumber, $netmask)){
+      return false;
+    }else{
+      return true;
+    }
   }
 
 
@@ -111,16 +123,42 @@ class termDNS extends plugin
   {
          /* Call parent execute */
     $smarty= get_smarty();
+
+    $tmp = $this->plInfo();
+    foreach($tmp['plProvidedAcls'] as $name => $translation){
+      $smarty->assign($name."ACL",$this->getacl($name));
+    }
+
     $display= "";
 
     $smarty->assign("staticAddress", ""); 
+    $smarty->assign("autonet", "");
  
+    /* Check for autonet button */
+    if ($this->autonet && isset($_POST['autonet'])){
+      $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
+      if(!empty($cmd) && $this->cn != ""){
+        $res = shell_exec($cmd." ".$this->cn);
+        if(!$res){
+          print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
+        } else {
+          $res= split(';', trim($res));
+          if (isset($res[0]) && $res[0] != ""){
+            $this->ipHostNumber= $res[0];
+          }
+          if (isset($res[1]) && $res[1] != ""){
+            $this->macAddress= $res[1];
+          }
+        }
+      }
+    }
+
     /* There is no dns available 
      */
     if($this->DNSenabled == false){
        
       /* Is IP address must ? */ 
-      $smarty->assign("DNS_is_account",false);  
+      $smarty->assign("DNS_enabled",false);  
       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
     
       /* Assign smarty all non DNs attributes */
@@ -128,12 +166,26 @@ class termDNS extends plugin
         $smarty->assign($attr,$this->$attr);
       }
       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
+
+      if ($this->autonet){
+        $smarty->assign("autonet", "true");
+      } else {
+        $smarty->assign("autonet", "");
+      }
       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
       return($display);
     }else{
-      $smarty->assign("DNS_is_account",true); 
+      $smarty->assign("DNS_enabled",true); 
     }
+
+    $accountACL = false; 
+    if($this->DNS_is_account && $this->acl_is_removeable()){
+      $accountACL = true;
+    }elseif(!$this->DNS_is_account && $this->acl_is_createable()){
+      $accountACL = true;
+    }
+    $smarty->assign("accountACL",$accountACL);
+
     /* Add new empty array to our record list */
     if(isset($_POST['AddNewRecord'])){
       $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
@@ -171,11 +223,23 @@ class termDNS extends plugin
     }
     
     /* Assign all needed vars */
-    $smarty->assign("DNSAccount",$this->DNS_is_account);
+    $smarty->assign("DNS_is_account",$this->DNS_is_account);
     $smarty->assign("Zones",$this->Zones);
     $smarty->assign("ZoneKeys",($this->Zones));
     $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
-  
+    /* Create zone array */
+    $idZones = array();
+    foreach($this->Zones as $id => $zone){
+      if($this->netmaskIsCoherent($id)) {
+        $idZones[$id] = $zone;
+      }else{
+        $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
+      }
+    }    
+    $smarty->assign("Zones",$idZones);
+    $smarty->assign("ZoneKeys", $this->Zones);
+
     $tmp = $this->generateRecordsList();
     
     $changeStateForRecords = $tmp['changeStateForRecords'];
@@ -188,61 +252,73 @@ class termDNS extends plugin
     return($display);
   }
 
+
   function remove_from_parent()
   {
-    /*
-    $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']);
+    if($this->initially_was_account){
+
+      $ldap = $this->config->get_ldap_link();
+
+      $tmp = array();
+      $this->dnsEntry['exists'] = false;
+      $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
+
+      /* Delete dns */
+      foreach($tmp['del'] as $dn => $del){
+        $ldap->cd($dn);
+        $ldap->rmdir_recursive($dn);
+      }
     }
-    */
   }
 
+
   /* Save data to object */
   function save_object()
   {
     /* Save all posted vars */
     plugin::save_object();
-    
-    /* Ge all non dns attributes (IP/MAC)*/
-    foreach($this->attributes as $attr){
-      if(isset($_POST[$attr])){
-        $this->$attr = $_POST[$attr];
-      }
-    }
 
-    /* Get dns attributes */
-    if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
+    if(isset($_POST['network_tpl_posted'])){
 
-      /* Check for posted record changes */
-      if(is_array($this->dnsEntry['RECORDS'])){
-        foreach($this->dnsEntry['RECORDS'] as $key => $value){
+      /* Ge all non dns attributes (IP/MAC)*/
+      foreach($this->attributes as $attr){
+        if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
+          $this->$attr = $_POST[$attr];
+        }
+      }
 
-          /* Check if type has changed */
-          if(isset($_POST['RecordTypeSelectedFor_'.$key])){
-            $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
-          }
-          /* Check if value has changed */
-          if(isset($_POST['RecordValue_'.$key])){
-            $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
+      /* Check if DNS should be enabled / disabled */
+      if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
+        $this->DNS_is_account = false;
+      }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
+        $this->DNS_is_account = true;
+      }
+
+      /* Get dns attributes */
+      if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
+
+        /* Check for posted record changes */
+        if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
+          foreach($this->dnsEntry['RECORDS'] as $key => $value){
+
+            /* Check if type has changed */
+            if(isset($_POST['RecordTypeSelectedFor_'.$key])){
+              $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
+            }
+            /* Check if value has changed */
+            if(isset($_POST['RecordValue_'.$key])){
+              $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
+            }
           }
         }
-      }
-      /* Get all basic DNS attributes (TTL, Clas ..)*/
-      foreach($this->DNSattributes as $attr){
-        if(isset($_POST[$attr])){
-          $this->dnsEntry[$attr] = $_POST[$attr];
+        /* Get all basic DNS attributes (TTL, Clas ..)*/
+        foreach($this->DNSattributes as $attr){
+          if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
+            $this->dnsEntry[$attr] = $_POST[$attr];
+          }
         }
-      }
 
-      /* Enable diable DNS */
-      if(isset($_POST['enableDNS'])){
-        $this->DNS_is_account = true;
-      }else{
-        $this->DNS_is_account = false;
+
       }
     }
   }
@@ -257,31 +333,21 @@ class termDNS extends plugin
     /* Check if ip must be given
      */  
     if(($this->IPisMust)||($this->DNS_is_account)){
-  
-      /* Check if ip is empty 
-       */
-      if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
+      if (empty($this->ipHostNumber)){
         $message[]= _("The required field 'IP-address' is not set.");
       }
 
-      /* check if given ip is valid ipi
-       */
-      $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
-      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
+      if (!is_ip($this->ipHostNumber)){
         $message[]= _("Wrong IP format in field IP-address.");
       }
     }
 
     /* Check if mac is empty 
      */
-    if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
+    if ($this->macAddress == "" ){
       $message[]= _("The required field 'MAC-address' is not set.");
     }
-
-    /* Check if given mac is valid mac 
-     */
-    $tr = count(split(":",$this->macAddress));
-    if($tr!=6){
+    if(!is_mac($this->macAddress)){
       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
     }
 
@@ -291,7 +357,13 @@ class termDNS extends plugin
       $checkArray = array();
       $onlyOnce   = array();
 
-      $onlyOnce['cNAMERecord'] = 0;
+      //  $onlyOnce['cNAMERecord'] = 0;
+       $tmp = array_flip($this->Zones);
+       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
+       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
+         $tmp2 = preg_replace("/^.*\//","",$tmp2);
+         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
+       }
 
       /* Walk through all entries and detect duplicates or mismatches
        */  
@@ -363,18 +435,19 @@ class termDNS extends plugin
     /* If isn't DNS account but initially was DNS account 
        remove all DNS entries 
      */ 
-    if(!$this->DNSenabled){
+    if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
       return;
     }else{
 
       /* Add ipHostNumber to aRecords
        */
+      $backup_dnsEntry = $this->dnsEntry;
       $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
 
       /* Create diff and follow instructions 
        * If Account was disabled, remove account by setting exists to false
        */
-      if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
+      if((!$this->DNS_is_account)&&($this->initially_was_account)){  
         $this->dnsEntry['exists'] = false;
         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
       }else{
@@ -411,8 +484,10 @@ class termDNS extends plugin
       /* Display errors 
        */
       if($ldap->get_error() != "Success"){
-        show_ldap_error("Record:".$ldap->get_error()); 
+        show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
       }
+
+      $this->dnsEntry =  $backup_dnsEntry;
     }
   }
 
@@ -423,7 +498,7 @@ class termDNS extends plugin
     $changeStateForRecords = "";
     
     if(!$this->DNS_is_account) {
-      $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
+      $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
       return $str;
     }
  
@@ -443,7 +518,7 @@ class termDNS extends plugin
 
     $str.= "  <tr>".
            "    <td colspan=2 width='50%'></td><td>".
-           "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
+           "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
            "    </td>".
            "  </tr>".
            "</table>";
@@ -467,6 +542,37 @@ class termDNS extends plugin
     $str.="</select>";
     return($str); 
   }
+
+
+  /* Return plugin informations for acl handling  */ 
+  function plInfo()
+  {
+    $tmp =  array(
+        "plShortName"   => _("DNS"),
+        "plDescription" => _("DNS settings"),
+        "plSelfModify"  => FALSE,
+        "plDepends"     => array(),
+        "plPriority"    => 5,
+        "plSection"     => array("administration"),
+        "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
+
+        "plProvidedAcls"=> array(
+          "ipHostNumber"  => _("IP address"),
+          "macAddress"    => _("MAC address"))
+        );
+
+    /* Hide all dns specific code, if dns is not available
+     */
+    $DNSenabled = false;
+    foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
+      if(preg_match("/^servdns$/",$tab['CLASS'])){
+        $tmp['plProvidedAcls']["Records"]        = _("DNS records");
+        $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
+        $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
+      }
+    }
+    return($tmp);
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: