Code

Fixed regex for opsi hosts
[gosa.git] / gosa-plugins / systems / admin / systems / class_termDNS.inc
index ef6f8252a6e8e8a7dd865b64230438044418027c..df1944d3eb519a0095a8a05ce9b8c1e6224186e9 100644 (file)
@@ -79,7 +79,9 @@ class termDNS extends plugin
     $this->IPisMust       = $IPisMust;
     $this->namingAttr     = $namingAttr;
 
-    plugin::plugin ($config, $parent->dn);
+    plugin::plugin ($config, $parent->dn, $this->parent);
+  
+    $this->attrs = &$this->parent->attrs;
 
     if(isset($this->attrs[$namingAttr][0])){
       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs[$namingAttr][0]);
@@ -324,7 +326,7 @@ class termDNS extends plugin
       $this->dialog->read_only     = !$this->acl_is_writeable("dhcpSetup");
       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
       if(!empty($this->ipHostNumber)){
-        $this->dialog->statements['fixed-address'] = $this->ipHostNumber
+        $this->dialog->statements->set('fixed-address', $this->ipHostNumber)
       }
     }
 
@@ -332,7 +334,7 @@ class termDNS extends plugin
       $this->dialog = FALSE; 
     }
 
-    if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup")){
+    if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup") && is_object($this->dialog)){
       $this->dialog->save_object();
       $msgs = $this->dialog->check(array());
       if(count($msgs)){
@@ -391,7 +393,7 @@ class termDNS extends plugin
         foreach($this->Zones as $key => $name){
           if($name == $this->dnsEntry['zoneName']){
             $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
-            $this->ipHostNumber = $this->generateRandomIp($net);
+            $this->ipHostNumber = $this->generateRandomIP($net);
           }
         }
       }
@@ -541,11 +543,15 @@ class termDNS extends plugin
         DNS posts
        ******/
 
-      /* Check if DNS should be enabled / disabled */
-      if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
-        $this->DNS_is_account = false;
-      }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
-        $this->DNS_is_account = true;
+      /* Check if DNS should be enabled / disabled 
+       *  -skip this, if the dns account is enforced.
+       */
+      if(!$this->hide_dns_check_box){
+        if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
+          $this->DNS_is_account = false;
+        }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
+          $this->DNS_is_account = true;
+        }
       }
 
       /* Get dns attributes */
@@ -736,7 +742,7 @@ class termDNS extends plugin
         $this->dialog->cn = $this->cn;
         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
         if(!empty($this->ipHostNumber)){
-          $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
+          $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
         }
         $this->dialog->execute();
         $this->dialog->save_object(); 
@@ -1105,13 +1111,50 @@ class termDNS extends plugin
   {
     $str = $net;
     $cnt = 4;
-    while(substr_count($str,".") < 3 && $cnt > 0){
-      $str .= ".".rand(0,255);
-      $str = preg_replace("/\.\.*/",".",$str);
-      $str = trim($str,". ");
-      $cnt --;
+
+    // first gather all IPs 
+    $ldap = $this->config->get_ldap_link();
+    $ocs = 
+      "(objectClass=goFonHardware)".
+      "(objectClass=goServer)".
+      "(objectClass=GOhard)".
+      "(objectClass=gotoTerminal)".
+      "(objectClass=gotoWorkstation)".
+      "(objectClass=gotoPrinter)".
+      "(objectClass=ipHost)";
+    $list = array();
+    $ldap->search("(&(|{$ocs})(ipHostNumber=*))",array("ipHostNumber"));
+    while($attrs = $ldap->fetch()){
+      if (preg_match("/^$net\./", $attrs['ipHostNumber'][0])) {
+        $list[] = $attrs['ipHostNumber'][0];
+      }
     }
-    return($str);
+
+    // Set starting ip.
+    $ip_data = preg_split("/\./",$net);
+    for($i=0;$i<4;$i++){
+      if(!isset($ip_data[$i])) $ip_data[$i] = 0;
+    }
+
+    // Search the next free and valid ip.
+    while(in_array(implode(".",$ip_data),$list) || $ip_data[3] <= 1){
+      $ip_data[3] ++ ;
+      if($ip_data[3] > 255){
+        $ip_data[3] = 1 ;
+        $ip_data[2] ++ ;
+      }
+      if($ip_data[2] > 255){
+        $ip_data[2] = 1 ;
+        $ip_data[1] ++ ;
+      }
+      if($ip_data[1] > 255){
+        $ip_data[1] = 1 ;
+        $ip_data[0] ++ ;
+      }
+      if($ip_data[0] > 255) break;
+    }
+
+    return(implode(".",$ip_data));
   }