Author: William Preston <wpreston@suse.com>
Subject: do not bind ddns socket in server when ddns-update-style is none
References: bsc#891655
Upstream: yes

backported from commit 61ef216b8dc05bc4245b61eee812038757d12ffe
by Shawn Routhier <sar@isc.org> with changes.

diff -prauN dhcp-4.2.4-P2/client/dhclient.c dhcp-4.2.4-P2/client/dhclient.c
--- dhcp-4.2.4-P2/client/dhclient.c	2014-09-01 12:54:00.000000000 +0200
+++ dhcp-4.2.4-P2/client/dhclient.c	2014-09-02 16:08:11.000000000 +0200
@@ -148,7 +148,7 @@ main(int argc, char **argv) {
 #endif
 
 	/* Set up the isc and dns library managers */
-	status = dhcp_context_create();
+	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB);
 	if (status != ISC_R_SUCCESS)
 		log_fatal("Can't initialize context: %s",
 			  isc_result_totext(status));
diff -prauN dhcp-4.2.4-P2/dhcpctl/dhcpctl.c dhcp-4.2.4-P2/dhcpctl/dhcpctl.c
--- dhcp-4.2.4-P2/dhcpctl/dhcpctl.c	2012-08-24 21:11:21.000000000 +0200
+++ dhcp-4.2.4-P2/dhcpctl/dhcpctl.c	2014-09-02 16:20:43.000000000 +0200
@@ -48,7 +48,7 @@ dhcpctl_status dhcpctl_initialize ()
 	isc_result_t status;
 
 	/* Set up the isc and dns library managers */
-	status = dhcp_context_create();
+	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB);
 	if (status != ISC_R_SUCCESS)
 		return status;
 
diff -prauN dhcp-4.2.4-P2/includes/omapip/isclib.h dhcp-4.2.4-P2/includes/omapip/isclib.h
--- dhcp-4.2.4-P2/includes/omapip/isclib.h	2012-08-24 21:11:21.000000000 +0200
+++ dhcp-4.2.4-P2/includes/omapip/isclib.h	2014-09-02 16:21:33.000000000 +0200
@@ -116,7 +116,9 @@ isclib_make_dst_key(char          *innam
 		    int            length,
 		    dst_key_t    **dstkey);
 
-isc_result_t dhcp_context_create(void);
+#define DHCP_CONTEXT_PRE_DB  1
+#define DHCP_CONTEXT_POST_DB 2
+isc_result_t dhcp_context_create(int              flags);
 void isclib_cleanup(void);
 
 #endif /* ISCLIB_H */
diff -prauN dhcp-4.2.4-P2/omapip/isclib.c dhcp-4.2.4-P2/omapip/isclib.c
--- dhcp-4.2.4-P2/omapip/isclib.c	2014-09-01 12:54:00.000000000 +0200
+++ dhcp-4.2.4-P2/omapip/isclib.c	2014-09-02 16:04:54.000000000 +0200
@@ -86,98 +86,102 @@ handle_signal(int sig, void (*handler)(i
 }
 
 isc_result_t
-dhcp_context_create(void) {
+dhcp_context_create(int flags) {
 	isc_result_t result;
 
-	/*
-	 * Set up the error messages, this isn't the right place
-	 * for this call but it is convienent for now.
-	 */
-	result = dhcp_result_register();
-	if (result != ISC_R_SUCCESS) {
-		log_fatal("register_table() %s: %u", "failed", result);
-	}
+	if ((flags & DHCP_CONTEXT_PRE_DB) != 0) {
+		/*
+		 * Set up the error messages, this isn't the right place
+		 * for this call but it is convienent for now.
+		 */
+		result = dhcp_result_register();
+		if (result != ISC_R_SUCCESS) {
+			log_fatal("register_table() %s: %u", "failed", result);
+		}
 
-	memset(&dhcp_gbl_ctx, 0, sizeof (dhcp_gbl_ctx));
+		memset(&dhcp_gbl_ctx, 0, sizeof (dhcp_gbl_ctx));
 	
-	isc_lib_register();
+		isc_lib_register();
 
-	/* get the current time for use as the random seed */
-	gettimeofday(&cur_tv, (struct timezone *)0);
-	isc_random_seed(cur_tv.tv_sec);
+		/* get the current time for use as the random seed */
+		gettimeofday(&cur_tv, (struct timezone *)0);
+		isc_random_seed(cur_tv.tv_sec);
 
 #if defined (NSUPDATE)
-	result = dns_lib_init();
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
+		result = dns_lib_init();
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+#else
+		/* The dst library is inited as part of dns_lib_init, we don't
+		 * need it if NSUPDATE is enabled */
+		result = dst_lib_init(dhcp_gbl_ctx.mctx, NULL, 0);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
 #endif
 
-	result = isc_mem_create(0, 0, &dhcp_gbl_ctx.mctx);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	result = isc_appctx_create(dhcp_gbl_ctx.mctx, &dhcp_gbl_ctx.actx);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	result = isc_app_ctxstart(dhcp_gbl_ctx.actx);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	/*
-	 * Always ignore SIGPIPE.
-	 * Otherwise we will die before the errno == EPIPE
-	 * checks in the socket code are reached.
-	 *
-	 * Note: unlike isc_app_start(), isc_app_ctxstart()
-	 *       does not set any signal handlers.
-	 */
-	result = handle_signal(SIGPIPE, SIG_IGN);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	dhcp_gbl_ctx.actx_started = ISC_TRUE;
-
-	result = isc_taskmgr_createinctx(dhcp_gbl_ctx.mctx,
-					 dhcp_gbl_ctx.actx,
-					 1, 0,
-					 &dhcp_gbl_ctx.taskmgr);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	result = isc_socketmgr_createinctx(dhcp_gbl_ctx.mctx,
-					   dhcp_gbl_ctx.actx,
-					   &dhcp_gbl_ctx.socketmgr);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	result = isc_timermgr_createinctx(dhcp_gbl_ctx.mctx,
-					  dhcp_gbl_ctx.actx,
-					  &dhcp_gbl_ctx.timermgr);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
-	result = isc_task_create(dhcp_gbl_ctx.taskmgr, 0, &dhcp_gbl_ctx.task);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
+		result = isc_mem_create(0, 0, &dhcp_gbl_ctx.mctx);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		result = isc_appctx_create(dhcp_gbl_ctx.mctx, &dhcp_gbl_ctx.actx);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		result = isc_app_ctxstart(dhcp_gbl_ctx.actx);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		/*
+		 * Always ignore SIGPIPE.
+		 * Otherwise we will die before the errno == EPIPE
+		 * checks in the socket code are reached.
+		 *
+		 * Note: unlike isc_app_start(), isc_app_ctxstart()
+		 *       does not set any signal handlers.
+		 */
+		result = handle_signal(SIGPIPE, SIG_IGN);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		dhcp_gbl_ctx.actx_started = ISC_TRUE;
+
+		result = isc_taskmgr_createinctx(dhcp_gbl_ctx.mctx,
+						 dhcp_gbl_ctx.actx,
+						 1, 0,
+						 &dhcp_gbl_ctx.taskmgr);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		result = isc_socketmgr_createinctx(dhcp_gbl_ctx.mctx,
+						   dhcp_gbl_ctx.actx,
+						   &dhcp_gbl_ctx.socketmgr);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		result = isc_timermgr_createinctx(dhcp_gbl_ctx.mctx,
+						  dhcp_gbl_ctx.actx,
+						  &dhcp_gbl_ctx.timermgr);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+
+		result = isc_task_create(dhcp_gbl_ctx.taskmgr, 0, &dhcp_gbl_ctx.task);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+	}
 
 #if defined (NSUPDATE)
-	result = dns_client_createx(dhcp_gbl_ctx.mctx,
-				    dhcp_gbl_ctx.actx,
-				    dhcp_gbl_ctx.taskmgr,
-				    dhcp_gbl_ctx.socketmgr,
-				    dhcp_gbl_ctx.timermgr,
-				    0,
-				    &dhcp_gbl_ctx.dnsclient);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-#else
-	/* The dst library is inited as part of dns_lib_init, we don't
-	 * need it if NSUPDATE is enabled */
-	result = dst_lib_init(dhcp_gbl_ctx.mctx, NULL, 0);
-	if (result != ISC_R_SUCCESS)
-		goto cleanup;
-
+	if ((flags & DHCP_CONTEXT_POST_DB) != 0) {
+ 
+		result = dns_client_createx(dhcp_gbl_ctx.mctx,
+					    dhcp_gbl_ctx.actx,
+					    dhcp_gbl_ctx.taskmgr,
+					    dhcp_gbl_ctx.socketmgr,
+					    dhcp_gbl_ctx.timermgr,
+					    0,
+					    &dhcp_gbl_ctx.dnsclient);
+		if (result != ISC_R_SUCCESS)
+			goto cleanup;
+	}
 #endif
 	return(ISC_R_SUCCESS);
 
diff -prauN dhcp-4.2.4-P2/omapip/test.c dhcp-4.2.4-P2/omapip/test.c
--- dhcp-4.2.4-P2/omapip/test.c	2012-08-24 21:11:21.000000000 +0200
+++ dhcp-4.2.4-P2/omapip/test.c	2014-09-02 16:22:53.000000000 +0200
@@ -51,7 +51,7 @@ int main (int argc, char **argv)
 	omapi_object_t *connection = (omapi_object_t*)0;
 	isc_result_t status;
 
-	status = dhcp_context_create();
+	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB);
 	if (status != ISC_R_SUCCESS) {
 		fprintf(stderr, "Can't initialize context: %s\n",
 			isc_result_totext(status));
diff -prauN dhcp-4.2.4-P2/relay/dhcrelay.c dhcp-4.2.4-P2/relay/dhcrelay.c
--- dhcp-4.2.4-P2/relay/dhcrelay.c	2014-09-01 12:54:00.000000000 +0200
+++ dhcp-4.2.4-P2/relay/dhcrelay.c	2014-09-02 16:09:43.000000000 +0200
@@ -200,7 +200,7 @@ main(int argc, char **argv) {
 #endif	
 
 	/* Set up the isc and dns library managers */
-	status = dhcp_context_create();
+	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB);
 	if (status != ISC_R_SUCCESS)
 		log_fatal("Can't initialize context: %s",
 			  isc_result_totext(status));
diff -prauN dhcp-4.2.4-P2/server/dhcpd.c dhcp-4.2.4-P2/server/dhcpd.c
--- dhcp-4.2.4-P2/server/dhcpd.c	2014-09-01 12:54:00.000000000 +0200
+++ dhcp-4.2.4-P2/server/dhcpd.c	2014-09-02 16:12:34.000000000 +0200
@@ -285,7 +285,7 @@ main(int argc, char **argv) {
                 close(fd);
 
 	/* Set up the isc and dns library managers */
-	status = dhcp_context_create();
+	status = dhcp_context_create(DHCP_CONTEXT_PRE_DB);
 	if (status != ISC_R_SUCCESS)
 		log_fatal("Can't initialize context: %s",
 			  isc_result_totext(status));
@@ -1097,6 +1097,11 @@ void postconf_initialization (int quiet)
 	if (ddns_update_style == DDNS_UPDATE_STYLE_AD_HOC) {
 		log_fatal("ddns-update-style ad_hoc no longer supported");
 	}
+
+	if (ddns_update_style != DDNS_UPDATE_STYLE_NONE && dhcp_context_create(DHCP_CONTEXT_POST_DB)
+	    != ISC_R_SUCCESS)
+		log_fatal("Unable to complete ddns initialization");
+
 #else
 	/* If we don't have support for updates compiled in tell the user */
 	if (ddns_update_style != DDNS_UPDATE_STYLE_NONE) {
