PATCH: better configuration schema

"Robin Hack" <[email protected]>
Newsgroups gmane.network.dns.bind9.dlz
Message-ID <[email protected]>
Hi,

I wrote small experimental patch which doing configuration of (not
only) sql dlz drivers better.
On this version only mysql and postgres drivers are supported.

This patch is applicable (and tested) against bind 9.4.2 source tree.

Old schema:

dlz "Mysql zone" {
      database "mysql
      {SELECT zone FROM records WHERE zone = '%zone%'}
      {SELECT ttl, type, mx_priority, IF(type = 'TXT',
CONCAT('\"',data,'\"'), data) AS data
       FROM records
      WHERE zone = '%zone%' AND host = '%record%' AND type <> 'SOA'
AND type <> 'NS'}
      {SELECT ttl, type, data, primary_ns, resp_contact, serial,
refresh, retry, expire, minimum
... etc etc ...

... little confusing and buggy for me :/


New schema:

dlz "Mysql zone" {
        database "mysql
        {host=localhost dbname=dns user=dns pass=dns}
        {authority=SELECT zone FROM records WHERE zone = '%zone%'}
        {lookup=SELECT ttl, type, mx_priority, IF(type = 'TXT',
CONCAT('\"',data,'\"'), data) AS data
         FROM records
         WHERE zone = '%zone%' AND host = '%record%' AND type <> 'SOA'
AND type <> 'NS'}
        {findzone=SELECT ttl, type, data, primary_ns, resp_contact,
serial, refresh, retry, expire, minimum
         FROM records
         WHERE zone = '%zone%' AND (type = 'SOA' OR type='NS')}
        {allnodes=SELECT ttl, type, host, mx_priority, IF(type =
'TXT', CONCAT('\"',data,'\"'), data) AS data, resp_contact, serial,
refresh, retry, expire, minimum
         FROM records
         WHERE zone = '%zone%' AND type <> 'SOA' AND type <> 'NS'}
       {allowxfr=SELECT zone FROM xfr where zone='%zone%' AND client =
'%client%'}";
};

But I still keep backward compatibility with old configuration schema,
if no one key is found.

Enjoy if you like :).

Robin Hack

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
Bind-dlz-testers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bind-dlz-testers
dlz_better_config.patch (application/octet-stream, 13.4 KB)
diff -ru ./bind-9.4.2/contrib/dlz/drivers/dlz_mysql_driver.c ./bind-9.4.2-patched/contrib/dlz/drivers/dlz_mysql_driver.c
--- ./bind-9.4.2/contrib/dlz/drivers/dlz_mysql_driver.c	2007-02-06 06:56:54.000000000 +0100
+++ ./bind-9.4.2-patched/contrib/dlz/drivers/dlz_mysql_driver.c	2008-04-30 00:52:36.000000000 +0200
@@ -790,9 +790,26 @@
 	int port;
 	MYSQL *dbc;
 	char *endp;
-	int j;
+	unsigned int j;
 	unsigned int flags = 0;
 
+	/* strings for params parsing */
+	const char *param_keys[] = {
+		"allnodes=",	/* 0 in sql_queries_str array */
+		"allowxfr=",
+		"authority=",
+		"findzone=",
+		"lookup=",
+		"countzone=",	/* 5 in sql_queries_str array */
+	};
+	/* pocet prvku param_keys */
+	unsigned int param_keys_count = sizeof(param_keys) / sizeof(char *);
+	char *sql_queries_str[param_keys_count];
+	unsigned int key_pos;
+
+	/* backward compatibility issues */
+	isc_result_t they_all_dead_dave = 1;
+
 	UNUSED(driverarg);
 	UNUSED(dlzname);
 
@@ -844,35 +861,70 @@
 		isc_mem_free(ns_g_mctx, tmp);
 	}
 
-	/* how many queries were passed in from config file? */
-	switch(argc) {
-	case 4:
-		result = build_sqldbinstance(ns_g_mctx, NULL, NULL, NULL,
-					     argv[2], argv[3], NULL, &dbi);
-		break;
-	case 5:
-		result = build_sqldbinstance(ns_g_mctx, NULL, NULL, argv[4],
-					     argv[2], argv[3], NULL, &dbi);
-		break;
-	case 6:
-		result = build_sqldbinstance(ns_g_mctx, argv[5], NULL, argv[4],
-					     argv[2], argv[3], NULL, &dbi);
-		break;
-	case 7:
-		result = build_sqldbinstance(ns_g_mctx, argv[5],
-					     argv[6], argv[4],
-					     argv[2], argv[3], NULL, &dbi);
-		break;
-	case 8:
-		result = build_sqldbinstance(ns_g_mctx, argv[5],
-					     argv[6], argv[4],
-					     argv[2], argv[3], argv[7], &dbi);
-		break;
-	default:
-		/* not really needed, should shut up compiler. */
-		result = ISC_R_FAILURE;
+	/* replacement of old code */
+	/* initialize sql-queries */
+	for(j = 0; j < param_keys_count; ++j)
+	{
+		sql_queries_str[j] = NULL;
 	}
 
+	they_all_dead_dave = parse_config_lines(ns_g_mctx, sql_queries_str, param_keys_count,
+						param_keys, argc, argv);
+
+	/* backward compatibility support */
+	if ( they_all_dead_dave == 0 ) {
+		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+			      "Using _NEW_ configration schema.");
+
+		/* debug output - keys and queries */
+		for(key_pos = 0; key_pos < param_keys_count; ++key_pos) {
+			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+				      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+				      "%s%s", param_keys[key_pos] ,sql_queries_str[key_pos]);
+		} /* end for */
+
+		result = build_sqldbinstance(ns_g_mctx, sql_queries_str[0],
+					     sql_queries_str[1], sql_queries_str[2],
+					     sql_queries_str[3], sql_queries_str[4],
+					     sql_queries_str[5], &dbi);
+	} else {
+		/* wake up old zombie code from grave! */
+		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+			      "Using _OLD_ configration schema.");
+
+		/* how many queries were passed in from config file? */
+
+		switch(argc) {
+		case 4:
+			result = build_sqldbinstance(ns_g_mctx, NULL, NULL, NULL,
+						     argv[2], argv[3], NULL, &dbi);
+			break;
+		case 5:
+			result = build_sqldbinstance(ns_g_mctx, NULL, NULL, argv[4],
+						     argv[2], argv[3], NULL, &dbi);
+			break;
+		case 6:
+			result = build_sqldbinstance(ns_g_mctx, argv[5], NULL, argv[4],
+						     argv[2], argv[3], NULL, &dbi);
+			break;
+		case 7:
+			result = build_sqldbinstance(ns_g_mctx, argv[5],
+						     argv[6], argv[4],
+						     argv[2], argv[3], NULL, &dbi);
+			break;
+		case 8:
+			result = build_sqldbinstance(ns_g_mctx, argv[5],
+						     argv[6], argv[4],
+						     argv[2], argv[3], argv[7], &dbi);
+			break;
+		default:
+			/* not really needed, should shut up compiler. */
+			result = ISC_R_FAILURE;
+		}
+	} /* if end */
+
 	/* unsuccessful?, log err msg and cleanup. */
 	if (result != ISC_R_SUCCESS) {
 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
@@ -945,8 +997,9 @@
 	goto cleanup;
 
  full_cleanup:
-
-	destroy_sqldbinstance(dbi);
+ 	/* strange bug ? */
+	if (dbi != NULL)
+		destroy_sqldbinstance(dbi);
 
  cleanup:
 
@@ -961,6 +1014,12 @@
 	if (socket != NULL)
 		isc_mem_free(ns_g_mctx, socket);
 
+	/* deinitialize sql_queries_str array */
+	for(j = 0; j < param_keys_count; ++j) {
+		if ( sql_queries_str[j] != NULL ) {
+			isc_mem_free(ns_g_mctx, sql_queries_str[j]);
+		}
+	}
 
 	return result;
 }
diff -ru ./bind-9.4.2/contrib/dlz/drivers/dlz_postgres_driver.c ./bind-9.4.2-patched/contrib/dlz/drivers/dlz_postgres_driver.c
--- ./bind-9.4.2/contrib/dlz/drivers/dlz_postgres_driver.c	2005-10-26 06:57:23.000000000 +0200
+++ ./bind-9.4.2-patched/contrib/dlz/drivers/dlz_postgres_driver.c	2008-04-30 00:53:05.000000000 +0200
@@ -1026,6 +1026,23 @@
 	dbinstance_t *dbi = NULL;
 	unsigned int j;
 
+	/* strings for params parsing */
+	const char *param_keys[] = {
+		"allnodes=",	/* 0 in sql_queries_str array */
+		"allowxfr=",
+		"authority=",
+		"findzone=",
+		"lookup=",
+		"countzone=",	/* 5 in sql_queries_str array */
+	};
+	/* pocet prvku param_keys */
+	unsigned int param_keys_count = sizeof(param_keys) / sizeof(char *);
+	char *sql_queries_str[param_keys_count];
+	unsigned int key_pos;
+
+	/* backward compatibility issues */
+	isc_result_t they_all_dead_dave = 1;
+
 #ifdef ISC_PLATFORM_USETHREADS
 	/* if multi-threaded, we need a few extra variables. */
 	int dbcount;
@@ -1035,6 +1052,7 @@
 
 #endif /* ISC_PLATFORM_USETHREADS */
 
+
 	UNUSED(driverarg);
 	UNUSED(dlzname);
 
@@ -1072,6 +1090,34 @@
 		return (ISC_R_FAILURE);
 	}
 
+	/* replacement of old code */
+	/* initialize sql-queries */
+	for(j = 0; j < param_keys_count; ++j)
+	{
+		sql_queries_str[j] = NULL;
+	}
+
+	they_all_dead_dave = parse_config_lines(ns_g_mctx, sql_queries_str, param_keys_count,
+						param_keys, argc, argv);
+
+	if ( they_all_dead_dave == 0 ) {
+		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+			      "Using _NEW_ configration schema.");
+
+		/* debug output - keys and queries */
+		for(key_pos = 0; key_pos < param_keys_count; ++key_pos) {
+			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+				      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+				      "%s%s", param_keys[key_pos] ,sql_queries_str[key_pos]);
+		} /* end for */
+	} else {
+		/* wake up old zombie code from grave! */
+		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
+			      "Using _OLD_ configration schema.");
+	}
+
 	/* multithreaded build can have multiple DB connections */
 #ifdef ISC_PLATFORM_USETHREADS
 
@@ -1100,47 +1146,55 @@
 	for (i=0; i < dbcount; i++) {
 
 #endif /* ISC_PLATFORM_USETHREADS */
-
-		/* how many queries were passed in from config file? */
-		switch(argc) {
-		case 5:
-			result = build_sqldbinstance(ns_g_mctx, NULL, NULL,
-						     NULL, argv[3], argv[4],
-						     NULL, &dbi);
-			break;
-		case 6:
-			result = build_sqldbinstance(ns_g_mctx, NULL, NULL,
-						     argv[5], argv[3], argv[4],
-						     NULL, &dbi);
-			break;
-		case 7:
-			result = build_sqldbinstance(ns_g_mctx, argv[6], NULL,
-						     argv[5], argv[3], argv[4],
-						     NULL, &dbi);
-			break;
-		case 8:
-			result = build_sqldbinstance(ns_g_mctx, argv[6],
-						     argv[7], argv[5], argv[3],
-						     argv[4], NULL, &dbi);
-			break;
-		default:
-			/* not really needed, should shut up compiler. */
-			result = ISC_R_FAILURE;
-		}
-
-
-		if (result == ISC_R_SUCCESS) {
-			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
-				      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
-				      "Postgres driver created database "
-				      "instance object.");
-		} else { /* unsuccessful?, log err msg and cleanup. */
-			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
-				      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
-				      "Postgres driver could not create "
-				      "database instance object.");
-			goto cleanup;
-		}
+		if ( they_all_dead_dave == 0 ) {
+			result = build_sqldbinstance(ns_g_mctx, sql_queries_str[0],
+						     sql_queries_str[1], sql_queries_str[2],
+						     sql_queries_str[3], sql_queries_str[4],
+						     sql_queries_str[5], &dbi);
+		} else {
+			/* wake up old zombie code from grave! */
+
+			/* how many queries were passed in from config file? */
+			switch(argc) {
+			case 5:
+				result = build_sqldbinstance(ns_g_mctx, NULL, NULL,
+							     NULL, argv[3], argv[4],
+							     NULL, &dbi);
+				break;
+			case 6:
+				result = build_sqldbinstance(ns_g_mctx, NULL, NULL,
+							     argv[5], argv[3], argv[4],
+							     NULL, &dbi);
+				break;
+			case 7:
+				result = build_sqldbinstance(ns_g_mctx, argv[6], NULL,
+							     argv[5], argv[3], argv[4],
+							     NULL, &dbi);
+				break;
+			case 8:
+				result = build_sqldbinstance(ns_g_mctx, argv[6],
+							     argv[7], argv[5], argv[3],
+							     argv[4], NULL, &dbi);
+				break;
+			default:
+				/* not really needed, should shut up compiler. */
+				result = ISC_R_FAILURE;
+			}
+
+
+			if (result == ISC_R_SUCCESS) {
+				isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+					      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
+					      "Postgres driver created database "
+					      "instance object.");
+			} else { /* unsuccessful?, log err msg and cleanup. */
+				isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
+					      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
+					      "Postgres driver could not create "
+					      "database instance object.");
+				goto cleanup;
+			}
+		} /* end if all dead */
 
 #ifdef ISC_PLATFORM_USETHREADS
 
@@ -1192,9 +1246,9 @@
 
 		/* set DBI = null for next loop through. */
 		dbi = NULL;
-	}	/* end for loop */
+	} /* end for loop */
 
-		/* set dbdata to the list we created. */
+	/* set dbdata to the list we created. */
 	*dbdata = dblist;
 
 #else /* ISC_PLATFORM_USETHREADS */
@@ -1219,6 +1273,12 @@
 	return(ISC_R_SUCCESS);
 
  cleanup:
+	/* deinitialize sql_queries_str array */
+	for(j = 0; j < param_keys_count; ++j) {
+		if ( sql_queries_str[j] != NULL ) {
+			isc_mem_free(ns_g_mctx, sql_queries_str[j]);
+		}
+	}
 
 #ifdef ISC_PLATFORM_USETHREADS
 	/*
diff -ru ./bind-9.4.2/contrib/dlz/drivers/include/dlz/sdlz_helper.h ./bind-9.4.2-patched/contrib/dlz/drivers/include/dlz/sdlz_helper.h
--- ./bind-9.4.2/contrib/dlz/drivers/include/dlz/sdlz_helper.h	2005-09-05 02:18:23.000000000 +0200
+++ ./bind-9.4.2-patched/contrib/dlz/drivers/include/dlz/sdlz_helper.h	2008-04-30 15:08:16.000000000 +0200
@@ -90,6 +90,10 @@
  */
 
 /* see the code in sdlz_helper.c for more information on these methods */
+isc_result_t
+sdlzh_parse_config_lines(isc_mem_t *mctx, char **sql_queries_str,
+			 unsigned int param_keys_count, const char **param_keys,
+	           	 unsigned int argc, char **argv);
 
 char *
 sdlzh_build_querystring(isc_mem_t *mctx, query_list_t *querylist);
@@ -106,12 +110,19 @@
 char *
 sdlzh_get_parameter_value(isc_mem_t *mctx, const char *input, const char* key);
 
+char *
+sdlzh_get_parameter_value_long(isc_mem_t *mctx, const char *input, const char* key,
+                               const char* check);
+
+
 /* Compatability with existing DLZ drivers */
 
 #define	build_querystring	sdlzh_build_querystring
 #define	build_sqldbinstance	sdlzh_build_sqldbinstance
 #define	destroy_sqldbinstance	sdlzh_destroy_sqldbinstance
+#define	parse_config_lines	sdlzh_parse_config_lines
 
-#define	getParameterValue(x,y)  sdlzh_get_parameter_value(ns_g_mctx, (x), (y))
+#define	getParameterValue(x,y)	    sdlzh_get_parameter_value(ns_g_mctx, (x), (y))
+#define	getParameterValueLong(x,y)  sdlzh_get_parameter_value_long(ns_g_mctx, (x), (y), (NULL))
 
 #endif /* SDLZHELPER_H */
diff -ru ./bind-9.4.2/contrib/dlz/drivers/sdlz_helper.c ./bind-9.4.2-patched/contrib/dlz/drivers/sdlz_helper.c
--- ./bind-9.4.2/contrib/dlz/drivers/sdlz_helper.c	2005-09-05 02:18:20.000000000 +0200
+++ ./bind-9.4.2-patched/contrib/dlz/drivers/sdlz_helper.c	2008-04-30 00:57:43.000000000 +0200
@@ -68,6 +68,47 @@
  * sdlz helper methods
  */
 
+/* 
+ * new configuration scheme
+ */
+isc_result_t
+sdlzh_parse_config_lines(isc_mem_t *mctx, char **sql_queries_str,
+			 unsigned int param_keys_count, const char **param_keys,
+			 unsigned int argc, char **argv)
+{
+	/* counters */
+	unsigned int tmp_argc;
+	unsigned int key_pos;
+	isc_result_t result = 1;
+
+	char *tmp = NULL;
+
+	REQUIRE(mctx != NULL);
+	REQUIRE(param_keys != NULL && *param_keys != NULL);
+
+
+	for(tmp_argc = 2; tmp_argc < argc; ++tmp_argc) {
+		for(key_pos = 0; key_pos < param_keys_count; ++key_pos) {
+				
+			tmp = sdlzh_get_parameter_value_long(mctx, argv[tmp_argc],
+							     param_keys[key_pos], NULL);
+			if ( tmp == NULL ) {
+				continue;
+			}
+
+			/* they_all_dead_dave */
+			result = 0;
+
+			sql_queries_str[key_pos] = tmp;
+
+			tmp = NULL;
+	
+		} /* end for */
+	} /* end for */
+
+	return result;
+}
+
 /*%
  * properly destroys a querylist by de-allocating the
  * memory for each query segment, and then the list itself
@@ -497,6 +538,13 @@
 char *
 sdlzh_get_parameter_value(isc_mem_t *mctx, const char *input, const char* key)
 {
+	return sdlzh_get_parameter_value_long(mctx, input, key, " ");
+}
+
+char *
+sdlzh_get_parameter_value_long(isc_mem_t *mctx, const char *input, const char* key,
+			       const char* check)
+{
 	int keylen;
 	char *keystart;
 	char value[255];
@@ -519,13 +567,14 @@
 
 	for (i = 0; i < 255; i++) {
 		value[i] = keystart[keylen + i];
-		if (value[i] == ' ' || value[i] == '\0') {
+		if (value[i] == '\0' || ( check != NULL && value[i] == check[0])) {
 			value[i] = '\0';
 			break;
 		}
 	}
 
 	return isc_mem_strdup(mctx, value);
+
 }
 
 #endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.