Blame SOURCES/bind-9.5-libidn.patch

900526
diff -up bind-9.7.0b1/bin/dig/dighost.c.libidn bind-9.7.0b1/bin/dig/dighost.c
900526
--- bind-9.7.0b1/bin/dig/dighost.c.libidn	2009-09-16 01:48:09.000000000 +0200
900526
+++ bind-9.7.0b1/bin/dig/dighost.c	2009-10-20 10:49:26.719056220 +0200
900526
@@ -44,6 +44,11 @@
900526
 #include <idn/api.h>
900526
 #endif
900526
 
900526
+#ifdef WITH_LIBIDN
900526
+#include <stringprep.h>
900526
+#include <idna.h>
900526
+#endif
900526
+
900526
 #include <dns/byaddr.h>
900526
 #ifdef DIG_SIGCHASE
900526
 #include <dns/callbacks.h>
900526
@@ -153,6 +158,14 @@ static void		idn_check_result(idn_result
900526
 int  idnoptions	= 0;
900526
 #endif
900526
 
900526
+#ifdef WITH_LIBIDN
900526
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char **to);
900526
+static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
900526
+static isc_result_t	output_filter (isc_buffer_t *buffer,
900526
+				       unsigned int used_org,
900526
+				       isc_boolean_t absolute);
900526
+#endif
900526
+
900526
 /*%
900526
  * Exit Codes:
900526
  *
900526
@@ -1184,6 +1197,9 @@ setup_system(void) {
900526
 	dig_searchlist_t *domain = NULL;
900526
 	lwres_result_t lwresult;
900526
 	unsigned int lwresflags;
900526
+#ifdef WITH_LIBIDN
900526
+	isc_result_t result;
900526
+#endif
900526
 
900526
 	debug("setup_system()");
900526
 
900526
@@ -1242,8 +1258,15 @@ setup_system(void) {
900526
 
900526
 #ifdef WITH_IDN
900526
 	initialize_idn();
900526
+	
900526
+#endif
900526
+#ifdef WITH_LIBIDN
900526
+	result = dns_name_settotextfilter(output_filter);
900526
+	check_result(result, "dns_name_settotextfilter");
900526
+#ifdef HAVE_SETLOCALE
900526
+	setlocale (LC_ALL, "");
900526
+#endif
900526
 #endif
900526
-
900526
 	if (keyfile[0] != 0)
900526
 		setup_file_key();
900526
 	else if (keysecret[0] != 0)
900526
@@ -1957,12 +1980,18 @@ setup_lookup(dig_lookup_t *lookup) {
900526
 	idn_result_t mr;
900526
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
900526
 #endif
900526
+#ifdef WITH_LIBIDN
900526
+	char *utf8_str = NULL, utf8_name[MXNAME], ascii_name[MXNAME];
900526
+#endif
900526
 
900526
 #ifdef WITH_IDN
900526
 	result = dns_name_settotextfilter(output_filter);
900526
 	check_result(result, "dns_name_settotextfilter");
900526
 #endif
900526
-
900526
+#ifdef WITH_LIBIDN
900526
+	result = dns_name_settotextfilter (output_filter);
900526
+	check_result(result, "dns_name_settotextfilter");
900526
+#endif
900526
 	REQUIRE(lookup != NULL);
900526
 	INSIST(!free_now);
900526
 
900526
@@ -1999,6 +2028,16 @@ setup_lookup(dig_lookup_t *lookup) {
900526
 	mr = idn_encodename(IDN_LOCALCONV | IDN_DELIMMAP, lookup->textname,
900526
 			    utf8_textname, sizeof(utf8_textname));
900526
 	idn_check_result(mr, "convert textname to UTF-8");
900526
+#elif defined (WITH_LIBIDN)
900526
+	result = libidn_locale_to_utf8 (lookup->textname, &utf8_str);
900526
+	check_result (result, "converting textname to UTF-8");
900526
+	len = strlen (utf8_str);
900526
+	if (len < MXNAME) {
900526
+		(void) strcpy (utf8_name, utf8_str);
900526
+	} else {
900526
+		fatal ("Too long name");
900526
+	}
900526
+	isc_mem_free (mctx, utf8_str);
900526
 #endif
900526
 
900526
 	/*
900526
@@ -2018,6 +2057,15 @@ setup_lookup(dig_lookup_t *lookup) {
900526
 			lookup->origin = ISC_LIST_HEAD(search_list);
900526
 			lookup->need_search = ISC_FALSE;
900526
 		}
900526
+#elif defined (WITH_LIBIDN)
900526
+		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
900526
+			lookup->origin = NULL; /* Force abs lookup */
900526
+			lookup->done_as_is = ISC_TRUE;
900526
+			lookup->need_search = usesearch;
900526
+		} else if (lookup->origin == NULL && usesearch) {
900526
+			lookup->origin = ISC_LIST_HEAD(search_list);
900526
+			lookup->need_search = ISC_FALSE;
900526
+		}
900526
 #else
900526
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
900526
 			lookup->origin = NULL; /* Force abs lookup */
900526
@@ -2044,6 +2092,20 @@ setup_lookup(dig_lookup_t *lookup) {
900526
 			    IDN_IDNCONV | IDN_LENCHECK, utf8_textname,
900526
 			    idn_textname, sizeof(idn_textname));
900526
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
900526
+#elif defined (WITH_LIBIDN)
900526
+	if (lookup->origin != NULL) {
900526
+		result = libidn_locale_to_utf8 (lookup->origin->origin, &utf8_str);
900526
+		check_result (result, "convert origin to UTF-8");
900526
+		if (len + strlen (utf8_str) + 1 < MXNAME) {
900526
+			utf8_name[len++] = '.';
900526
+			(void) strcpy (utf8_name + len, utf8_str);
900526
+		} else {
900526
+			fatal ("Too long name + origin");
900526
+		}
900526
+		isc_mem_free (mctx, utf8_str);
900526
+	}
900526
+
900526
+	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
900526
 #else
900526
 	if (lookup->origin != NULL) {
900526
 		debug("trying origin %s", lookup->origin->origin);
900526
@@ -2099,6 +2161,13 @@ setup_lookup(dig_lookup_t *lookup) {
900526
 			result = dns_name_fromtext(lookup->name, &b,
900526
 						   dns_rootname, 0,
900526
 						   &lookup->namebuf);
900526
+#elif defined (WITH_LIBIDN)
900526
+			len = strlen (ascii_name);
900526
+			isc_buffer_init(&b, ascii_name, len);
900526
+			isc_buffer_add(&b, len);
900526
+			result = dns_name_fromtext(lookup->name, &b,
900526
+						   dns_rootname, 0,
900526
+						   &lookup->namebuf);
900526
 #else
900526
 			len = strlen(lookup->textname);
900526
 			isc_buffer_init(&b, lookup->textname, len);
900526
@@ -3617,7 +3686,7 @@ destroy_libs(void) {
900526
 	void * ptr;
900526
 	dig_message_t *chase_msg;
900526
 #endif
900526
-#ifdef WITH_IDN
900526
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
900526
 	isc_result_t result;
900526
 #endif
900526
 
900526
@@ -3656,6 +3725,10 @@ destroy_libs(void) {
900526
 	result = dns_name_settotextfilter(NULL);
900526
 	check_result(result, "dns_name_settotextfilter");
900526
 #endif
900526
+#ifdef WITH_LIBIDN
900526
+	result = dns_name_settotextfilter (NULL);
900526
+	check_result(result, "clearing dns_name_settotextfilter");
900526
+#endif
900526
 	dns_name_destroy();
900526
 
900526
 	if (commctx != NULL) {
900526
@@ -3834,6 +3907,79 @@ idn_check_result(idn_result_t r, const c
900526
 	}
900526
 }
900526
 #endif /* WITH_IDN */
900526
+#ifdef WITH_LIBIDN
900526
+/* If stringprep_locale_to_utf8 fails simple copy string */
900526
+static isc_result_t
900526
+libidn_locale_to_utf8 (const char *from, char **to) {
900526
+	char *utf8_str;
900526
+
900526
+	utf8_str = stringprep_locale_to_utf8 (from);
900526
+	if (utf8_str == NULL) {
900526
+		*to = isc_mem_allocate (mctx, strlen (from) + 1);
900526
+		if (*to == NULL)
900526
+			return (ISC_R_NOMEMORY);
900526
+		(void) strcpy (*to, from);
900526
+	} else {
900526
+		*to = isc_mem_allocate (mctx, strlen (utf8_str) + 1);
900526
+		if (*to == NULL)
900526
+			return (ISC_R_NOMEMORY);
900526
+		(void) strcpy (*to, utf8_str);
900526
+		free (utf8_str);
900526
+	}
900526
+	return (ISC_R_SUCCESS);
900526
+}
900526
+static isc_result_t
900526
+libidn_utf8_to_ascii (const char *from, char *to) {
900526
+	char *ascii;
900526
+
900526
+	if (idna_to_ascii_8z (from, &ascii, 0) != IDNA_SUCCESS)
900526
+		return (ISC_R_FAILURE);
900526
+
900526
+	(void) strcpy (to, ascii);
900526
+	free (ascii);
900526
+	return (ISC_R_SUCCESS);
900526
+}
900526
+/* based on idnkit's code*/
900526
+static isc_result_t
900526
+output_filter (isc_buffer_t *buffer, unsigned int used_org,
900526
+	       isc_boolean_t absolute) {
900526
+	char tmp1[MXNAME], *tmp2;
900526
+        size_t fromlen, tolen;
900526
+        isc_boolean_t end_with_dot;
900526
+
900526
+        fromlen = isc_buffer_usedlength(buffer) - used_org;
900526
+	if (fromlen >= MXNAME)
900526
+		return (ISC_R_SUCCESS);
900526
+        memcpy(tmp1, (char *)isc_buffer_base(buffer) + used_org, fromlen);
900526
+        end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
900526
+        if (absolute && !end_with_dot) {
900526
+                fromlen++;
900526
+		if (fromlen >= MXNAME)
900526
+			return (ISC_R_SUCCESS);
900526
+                tmp1[fromlen - 1] = '.';
900526
+        }
900526
+        tmp1[fromlen] = '\0';
900526
+
900526
+	if (idna_to_unicode_lzlz (tmp1, &tmp2, 0) != IDNA_SUCCESS)
900526
+		return (ISC_R_SUCCESS);
900526
+
900526
+	(void) strcpy (tmp1, tmp2);
900526
+	free (tmp2);
900526
+
900526
+        tolen = strlen(tmp1);
900526
+        if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
900526
+                tolen--;
900526
+
900526
+        if (isc_buffer_length(buffer) < used_org + tolen)
900526
+                return (ISC_R_NOSPACE);
900526
+
900526
+        isc_buffer_subtract(buffer, isc_buffer_usedlength(buffer) - used_org);
900526
+        memcpy(isc_buffer_used(buffer), tmp1, tolen);
900526
+        isc_buffer_add(buffer, tolen);
900526
+
900526
+        return (ISC_R_SUCCESS);
900526
+}
900526
+#endif /* WITH_LIBIDN*/
900526
 
900526
 #ifdef DIG_SIGCHASE
900526
 void
900526
diff -up bind-9.7.0b1/bin/dig/Makefile.in.libidn bind-9.7.0b1/bin/dig/Makefile.in
900526
--- bind-9.7.0b1/bin/dig/Makefile.in.libidn	2009-09-22 10:47:55.000000000 +0200
900526
+++ bind-9.7.0b1/bin/dig/Makefile.in	2009-10-20 10:50:06.201543709 +0200
900526
@@ -46,10 +46,10 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} 
900526
 		${LWRESDEPLIBS}
900526
 
900526
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
900526
-		${ISCLIBS} @IDNLIBS@ @LIBS@
900526
+		${ISCLIBS} @IDNLIBS@ @LIBS@ -lidn
900526
 
900526
 NOSYMLIBS =	${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
900526
-		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@
900526
+		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@ -lidn
900526
 
900526
 SUBDIRS =
900526
 
900526
@@ -67,6 +67,8 @@ HTMLPAGES =	dig.html host.html nslookup.
900526
 
900526
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
900526
 
900526
+EXT_CFLAGS = -DWITH_LIBIDN
900526
+
900526
 @BIND9_MAKE_RULES@
900526
 
900526
 dig@EXEEXT@: dig.@O@ dighost.@O@ ${UOBJS} ${DEPLIBS}