From b31bb1f27f066a2d49f5ab9ee0ca15e985efc788 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Mon, 26 Sep 2011 06:50:23 -0400
Subject: [PATCH] Fixed: FS#1619 - Add apache SNI / SAN support for SSL.

---
 server/plugins-available/bind_plugin.inc.php |   83 +++++++++++++++++++++++++++++++++++------
 1 files changed, 70 insertions(+), 13 deletions(-)

diff --git a/server/plugins-available/bind_plugin.inc.php b/server/plugins-available/bind_plugin.inc.php
index 2e4e334..9d67eda 100644
--- a/server/plugins-available/bind_plugin.inc.php
+++ b/server/plugins-available/bind_plugin.inc.php
@@ -105,11 +105,18 @@
 			$records = $app->db->queryAllRecords("SELECT * FROM dns_rr WHERE zone = ".$zone['id']." AND active = 'Y'");
 			$tpl->setLoop('zones',$records);
 			
-		
-			$filename = escapeshellcmd($dns_config['bind_zonefiles_dir'].'/pri.'.substr($zone['origin'],0,-1));
+			//TODO : change this when distribution information has been integrated into server record
+        	if (file_exists('/etc/gentoo-release')) {
+        		$filename = escapeshellcmd($dns_config['bind_zonefiles_dir'].'/pri/'.substr($zone['origin'],0,-1));
+        	}
+        	else {
+        		$filename = escapeshellcmd($dns_config['bind_zonefiles_dir'].'/pri.'.substr($zone['origin'],0,-1));
+        	}
+        	
 			$app->log("Writing BIND domain file: ".$filename,LOGLEVEL_DEBUG);
 			file_put_contents($filename,$tpl->grab());
-			exec('chown '.escapeshellcmd($dns_config['bind_user']).':'.escapeshellcmd($dns_config['bind_group']).' '.$filename);
+			chown($filename, escapeshellcmd($dns_config['bind_user']));
+			chgrp($filename, escapeshellcmd($dns_config['bind_group']));
 			unset($tpl);
 			unset($records);
 			unset($records_out);
@@ -123,7 +130,14 @@
 		
 		//* Delete old domain file, if domain name has been changed
 		if($data['old']['origin'] != $data['new']['origin']) {
-			$filename = $dns_config['bind_zonefiles_dir'].'/pri.'.substr($data['old']['origin'],0,-1);
+			//TODO : change this when distribution information has been integrated into server record
+        	if (file_exists('/etc/gentoo-release')) {
+        		$filename = $dns_config['bind_zonefiles_dir'].'/pri/'.substr($data['old']['origin'],0,-1);
+        	}
+        	else {
+        		$filename = $dns_config['bind_zonefiles_dir'].'/pri.'.substr($data['old']['origin'],0,-1);
+        	}
+			
 			if(is_file($filename)) unset($filename);
 		}
 		
@@ -143,7 +157,14 @@
 		$this->write_named_conf($data,$dns_config);
 		
 		//* Delete the domain file
-		$zone_file_name = $dns_config['bind_zonefiles_dir'].'/pri.'.substr($data['old']['origin'],0,-1);
+		//TODO : change this when distribution information has been integrated into server record
+        if (file_exists('/etc/gentoo-release')) {
+        	$zone_file_name = $dns_config['bind_zonefiles_dir'].'/pri/'.substr($data['old']['origin'],0,-1);
+        }
+        else {
+        	$zone_file_name = $dns_config['bind_zonefiles_dir'].'/pri.'.substr($data['old']['origin'],0,-1);
+        }
+		
 		if(is_file($zone_file_name)) unlink($zone_file_name);
 		$app->log("Deleting BIND domain file: ".$zone_file_name,LOGLEVEL_DEBUG);
 		
@@ -176,7 +197,14 @@
 		
 		//* Delete old domain file, if domain name has been changed
 		if($data['old']['origin'] != $data['new']['origin']) {
-			$filename = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.substr($data['old']['origin'],0,-1);
+			//TODO : change this when distribution information has been integrated into server record
+	        if (file_exists('/etc/gentoo-release')) {
+	        	$filename = $dns_config['bind_zonefiles_dir'].'/sec/'.substr($data['old']['origin'],0,-1);
+	        }
+	        else {
+	        	$filename = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.substr($data['old']['origin'],0,-1);
+	        }
+			
 			if(is_file($filename)) unset($filename);
 		}
 		
@@ -197,7 +225,14 @@
 		$this->write_named_conf($data,$dns_config);
 		
 		//* Delete the domain file
-		$zone_file_name = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.substr($data['old']['origin'],0,-1);
+		//TODO : change this when distribution information has been integrated into server record
+	    if (file_exists('/etc/gentoo-release')) {
+	    	$zone_file_name = $dns_config['bind_zonefiles_dir'].'/sec/'.substr($data['old']['origin'],0,-1);
+	    }
+	    else {
+	    	$zone_file_name = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.substr($data['old']['origin'],0,-1);
+	    }
+		
 		if(is_file($zone_file_name)) unlink($zone_file_name);
 		$app->log("Deleting BIND domain file for secondary zone: ".$zone_file_name,LOGLEVEL_DEBUG);
 		
@@ -235,7 +270,7 @@
 		global $app, $conf;
 		
 		//* Get the data of the soa and call soa_update
-		$tmp = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE id = ".$data['old']['zone']);
+		$tmp = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE id = ".intval($data['old']['zone']));
 		$data["new"] = $tmp;
 		$data["old"] = $tmp;
 		$this->action = 'update';
@@ -251,7 +286,27 @@
 		//* Only write the master file for the current server	
 		$tmps = $app->db->queryAllRecords("SELECT origin, xfer, also_notify FROM dns_soa WHERE active = 'Y' AND server_id=".$conf["server_id"]);
 		$zones = array();
+		
+		//* Check if the current zone that triggered this function has at least one NS record
+		$rec_num = $app->db->queryOneRecord("SELECT count(id) as ns FROM dns_rr WHERE type = 'NS' AND zone = ".intval($data['new']['id'])." AND active = 'Y'");
+		if($rec_num['ns'] == 0) {
+			$exclude_zone = $data['new']['origin'];
+		} else {
+			$exclude_zone = '';
+		}
+		
+		//TODO : change this when distribution information has been integrated into server record
+	    if (file_exists('/etc/gentoo-release')) {
+	    	$pri_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/pri/';
+	    	$sec_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/sec/';
+	    	
+	    }
+	    else {
+	    	$pri_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/pri.';
+	    	$sec_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/slave/sec.';
+	    }
 
+		//* Loop trough zones
 		foreach($tmps as $tmp) {
 			
 			$options = '';
@@ -262,10 +317,12 @@
 			}
 			if(trim($tmp['also_notify']) != '') $options .= '        also-notify {'.str_replace(',',';',$tmp['also_notify']).";};\n";
 			
-			$zones[] = array(	'zone' => substr($tmp['origin'],0,-1),
-								'zonefile_path' => $dns_config['bind_zonefiles_dir'].'/pri.'.substr($tmp['origin'],0,-1),
-								'options' => $options
-							);
+			if($tmp['origin'] != $exclude_zone) {
+				$zones[] = array(	'zone' => substr($tmp['origin'],0,-1),
+									'zonefile_path' => $pri_zonefiles_path.substr($tmp['origin'],0,-1),
+									'options' => $options
+								);
+			}
 		}
 
 		$tpl = new tpl();
@@ -287,7 +344,7 @@
 
 			
 			$zones_sec[] = array(	'zone' => substr($tmp['origin'],0,-1),
-									'zonefile_path' => $dns_config['bind_zonefiles_dir'].'/slave/sec.'.substr($tmp['origin'],0,-1),
+									'zonefile_path' => $sec_zonefiles_path.substr($tmp['origin'],0,-1),
 									'options' => $options
 								);
 

--
Gitblit v1.9.1