Adam Tkac 3416d0
diff -up bind-9.5.0a6/bin/dig/Makefile.in.libidn bind-9.5.0a6/bin/dig/Makefile.in
Adam Tkac 3416d0
--- bind-9.5.0a6/bin/dig/Makefile.in.libidn	2007-07-29 13:17:00.000000000 +0200
Adam Tkac 3416d0
+++ bind-9.5.0a6/bin/dig/Makefile.in	2007-07-29 13:17:01.000000000 +0200
Adam Tkac 3416d0
@@ -45,7 +45,7 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} 
Adam Tkac 3416d0
 		${LWRESDEPLIBS}
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCLIBS} \
Adam Tkac 3416d0
-		${ISCCFGLIBS} @IDNLIBS@ @LIBS@
Adam Tkac 3416d0
+		${ISCCFGLIBS} @IDNLIBS@ @LIBS@ -lidn
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 SUBDIRS =
Adam Tkac 3416d0
 
Adam Tkac 3416d0
@@ -63,7 +63,7 @@ HTMLPAGES =	dig.html host.html nslookup.
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
Adam Tkac 3416d0
 
Adam Tkac 3416d0
-EXT_CFLAGS = -fPIE
Adam Tkac 3416d0
+EXT_CFLAGS = -fPIE -DWITH_LIBIDN
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 @BIND9_MAKE_RULES@
Adam Tkac 3416d0
 
Adam Tkac 3416d0
diff -up bind-9.5.0a6/bin/dig/dighost.c.libidn bind-9.5.0a6/bin/dig/dighost.c
Adam Tkac 3416d0
--- bind-9.5.0a6/bin/dig/dighost.c.libidn	2007-06-19 01:47:17.000000000 +0200
Adam Tkac 3416d0
+++ bind-9.5.0a6/bin/dig/dighost.c	2007-07-30 14:58:19.000000000 +0200
Adam Tkac 3416d0
@@ -44,6 +44,11 @@
Adam Tkac 3416d0
 #include <idn/api.h>
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+#include <stringprep.h>
Adam Tkac 3416d0
+#include <idna.h>
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+
Adam Tkac 3416d0
 #include <dns/byaddr.h>
Adam Tkac 3416d0
 #ifdef DIG_SIGCHASE
Adam Tkac 3416d0
 #include <dns/dnssec.h>
Adam Tkac 3416d0
@@ -147,6 +152,14 @@ static void		idn_check_result(idn_result
Adam Tkac 3416d0
 int  idnoptions	= 0;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char **to);
Adam Tkac 3416d0
+static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
Adam Tkac 3416d0
+static isc_result_t	output_filter (isc_buffer_t *buffer,
Adam Tkac 3416d0
+				       unsigned int used_org,
Adam Tkac 3416d0
+				       isc_boolean_t absolute);
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+
Adam Tkac 3416d0
 /*%
Adam Tkac 3416d0
  * Exit Codes:
Adam Tkac 3416d0
  *
Adam Tkac 3416d0
@@ -1002,6 +1015,9 @@ void
Adam Tkac 3416d0
 setup_system(void) {
Adam Tkac 3416d0
 	dig_searchlist_t *domain = NULL;
Adam Tkac 3416d0
 	lwres_result_t lwresult;
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	isc_result_t result;
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 	debug("setup_system()");
Adam Tkac 3416d0
 
Adam Tkac 3416d0
@@ -1050,8 +1066,15 @@ setup_system(void) {
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 #ifdef WITH_IDN
Adam Tkac 3416d0
 	initialize_idn();
Adam Tkac 3416d0
+	
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	result = dns_name_settotextfilter(output_filter);
Adam Tkac 3416d0
+	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
+#ifdef HAVE_SETLOCALE
Adam Tkac 3416d0
+	setlocale (LC_ALL, "");
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-
Adam Tkac 3416d0
 	if (keyfile[0] != 0)
Adam Tkac 3416d0
 		setup_file_key();
Adam Tkac 3416d0
 	else if (keysecret[0] != 0)
Adam Tkac 3416d0
@@ -1740,12 +1763,18 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 	idn_result_t mr;
Adam Tkac 3416d0
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	char *utf8_str = NULL, utf8_name[MXNAME], ascii_name[MXNAME];
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 #ifdef WITH_IDN
Adam Tkac 3416d0
 	result = dns_name_settotextfilter(output_filter);
Adam Tkac 3416d0
 	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	result = dns_name_settotextfilter (output_filter);
Adam Tkac 3416d0
+	check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 	REQUIRE(lookup != NULL);
Adam Tkac 3416d0
 	INSIST(!free_now);
Adam Tkac 3416d0
 
Adam Tkac 3416d0
@@ -1782,6 +1811,16 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 	mr = idn_encodename(IDN_LOCALCONV | IDN_DELIMMAP, lookup->textname,
Adam Tkac 3416d0
 			    utf8_textname, sizeof(utf8_textname));
Adam Tkac 3416d0
 	idn_check_result(mr, "convert textname to UTF-8");
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+	result = libidn_locale_to_utf8 (lookup->textname, &utf8_str);
Adam Tkac 3416d0
+	check_result (result, "converting textname to UTF-8");
Adam Tkac 3416d0
+	len = strlen (utf8_str);
Adam Tkac 3416d0
+	if (len < MXNAME) {
Adam Tkac 3416d0
+		(void) strcpy (utf8_name, utf8_str);
Adam Tkac 3416d0
+	} else {
Adam Tkac 3416d0
+		fatal ("Too long name");
Adam Tkac 3416d0
+	}
Adam Tkac 3416d0
+	isc_mem_free (mctx, utf8_str);
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 	/*
Adam Tkac 3416d0
@@ -1801,6 +1840,15 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 			lookup->origin = ISC_LIST_HEAD(search_list);
Adam Tkac 3416d0
 			lookup->need_search = ISC_FALSE;
Adam Tkac 3416d0
 		}
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
Adam Tkac 3416d0
+			lookup->origin = NULL; /* Force abs lookup */
Adam Tkac 3416d0
+			lookup->done_as_is = ISC_TRUE;
Adam Tkac 3416d0
+			lookup->need_search = usesearch;
Adam Tkac 3416d0
+		} else if (lookup->origin == NULL && usesearch) {
Adam Tkac 3416d0
+			lookup->origin = ISC_LIST_HEAD(search_list);
Adam Tkac 3416d0
+			lookup->need_search = ISC_FALSE;
Adam Tkac 3416d0
+		}
Adam Tkac 3416d0
 #else
Adam Tkac 3416d0
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
Adam Tkac 3416d0
 			lookup->origin = NULL; /* Force abs lookup */
Adam Tkac 3416d0
@@ -1827,6 +1875,20 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 			    IDN_IDNCONV | IDN_LENCHECK, utf8_textname,
Adam Tkac 3416d0
 			    idn_textname, sizeof(idn_textname));
Adam Tkac 3416d0
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+	if (lookup->origin != NULL) {
Adam Tkac 3416d0
+		result = libidn_locale_to_utf8 (lookup->origin->origin, &utf8_str);
Adam Tkac 3416d0
+		check_result (result, "convert origin to UTF-8");
Adam Tkac 3416d0
+		if (len + strlen (utf8_str) + 1 < MXNAME) {
Adam Tkac 3416d0
+			utf8_name[len++] = '.';
Adam Tkac 3416d0
+			(void) strcpy (utf8_name + len, utf8_str);
Adam Tkac 3416d0
+		} else {
Adam Tkac 3416d0
+			fatal ("Too long name + origin");
Adam Tkac 3416d0
+		}
Adam Tkac 3416d0
+		isc_mem_free (mctx, utf8_str);
Adam Tkac 3416d0
+	}
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
Adam Tkac 3416d0
 #else
Adam Tkac 3416d0
 	if (lookup->origin != NULL) {
Adam Tkac 3416d0
 		debug("trying origin %s", lookup->origin->origin);
Adam Tkac 3416d0
@@ -1883,6 +1945,14 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 3416d0
 						   dns_rootname,
Adam Tkac 3416d0
 						   ISC_FALSE,
Adam Tkac 3416d0
 						   &lookup->namebuf);
Adam Tkac 3416d0
+#elif defined (WITH_LIBIDN)
Adam Tkac 3416d0
+			len = strlen (ascii_name);
Adam Tkac 3416d0
+			isc_buffer_init(&b, ascii_name, len);
Adam Tkac 3416d0
+			isc_buffer_add(&b, len);
Adam Tkac 3416d0
+			result = dns_name_fromtext(lookup->name, &b,
Adam Tkac 3416d0
+						   dns_rootname,
Adam Tkac 3416d0
+						   ISC_FALSE,
Adam Tkac 3416d0
+						   &lookup->namebuf);
Adam Tkac 3416d0
 #else
Adam Tkac 3416d0
 			len = strlen(lookup->textname);
Adam Tkac 3416d0
 			isc_buffer_init(&b, lookup->textname, len);
Adam Tkac 3416d0
@@ -3310,7 +3380,7 @@ destroy_libs(void) {
Adam Tkac 3416d0
 	void * ptr;
Adam Tkac 3416d0
 	dig_message_t *chase_msg;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
-#ifdef WITH_IDN
Adam Tkac 3416d0
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
Adam Tkac 3416d0
 	isc_result_t result;
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
 
Adam Tkac 3416d0
@@ -3349,6 +3419,10 @@ destroy_libs(void) {
Adam Tkac 3416d0
         result = dns_name_settotextfilter(NULL);
Adam Tkac 3416d0
         check_result(result, "dns_name_settotextfilter");
Adam Tkac 3416d0
 #endif
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+	result = dns_name_settotextfilter (NULL);
Adam Tkac 3416d0
+	check_result(result, "clearing dns_name_settotextfilter");
Adam Tkac 3416d0
+#endif
Adam Tkac 3416d0
 	dns_name_destroy();
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 	if (commctx != NULL) {
Adam Tkac 3416d0
@@ -3525,6 +3599,79 @@ idn_check_result(idn_result_t r, const c
Adam Tkac 3416d0
 	}
Adam Tkac 3416d0
 }
Adam Tkac 3416d0
 #endif /* WITH_IDN */
Adam Tkac 3416d0
+#ifdef WITH_LIBIDN
Adam Tkac 3416d0
+/* If stringprep_locale_to_utf8 fails simple copy string */
Adam Tkac 3416d0
+static isc_result_t
Adam Tkac 3416d0
+libidn_locale_to_utf8 (const char *from, char **to) {
Adam Tkac 3416d0
+	char *utf8_str;
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	utf8_str = stringprep_locale_to_utf8 (from);
Adam Tkac 3416d0
+	if (utf8_str == NULL) {
Adam Tkac 3416d0
+		*to = isc_mem_allocate (mctx, strlen (from) + 1);
Adam Tkac 3416d0
+		if (*to == NULL)
Adam Tkac 3416d0
+			return (ISC_R_NOMEMORY);
Adam Tkac 3416d0
+		(void) strcpy (*to, from);
Adam Tkac 3416d0
+	} else {
Adam Tkac 3416d0
+		*to = isc_mem_allocate (mctx, strlen (utf8_str) + 1);
Adam Tkac 3416d0
+		if (*to == NULL)
Adam Tkac 3416d0
+			return (ISC_R_NOMEMORY);
Adam Tkac 3416d0
+		(void) strcpy (*to, utf8_str);
Adam Tkac 3416d0
+		free (utf8_str);
Adam Tkac 3416d0
+	}
Adam Tkac 3416d0
+	return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+}
Adam Tkac 3416d0
+static isc_result_t
Adam Tkac 3416d0
+libidn_utf8_to_ascii (const char *from, char *to) {
Adam Tkac 3416d0
+	char *ascii;
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	if (idna_to_ascii_8z (from, &ascii, 0) != IDNA_SUCCESS)
Adam Tkac 3416d0
+		return (ISC_R_FAILURE);
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	(void) strcpy (to, ascii);
Adam Tkac 3416d0
+	free (ascii);
Adam Tkac 3416d0
+	return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+}
Adam Tkac 3416d0
+/* based on idnkit's code*/
Adam Tkac 3416d0
+static isc_result_t
Adam Tkac 3416d0
+output_filter (isc_buffer_t *buffer, unsigned int used_org,
Adam Tkac 3416d0
+	       isc_boolean_t absolute) {
Adam Tkac 3416d0
+	char tmp1[MXNAME], *tmp2;
Adam Tkac 3416d0
+        size_t fromlen, tolen;
Adam Tkac 3416d0
+        isc_boolean_t end_with_dot;
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+        fromlen = isc_buffer_usedlength(buffer) - used_org;
Adam Tkac 3416d0
+	if (fromlen >= MXNAME)
Adam Tkac 3416d0
+		return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+        memcpy(tmp1, (char *)isc_buffer_base(buffer) + used_org, fromlen);
Adam Tkac 3416d0
+        end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
Adam Tkac 3416d0
+        if (absolute && !end_with_dot) {
Adam Tkac 3416d0
+                fromlen++;
Adam Tkac 3416d0
+		if (fromlen >= MXNAME)
Adam Tkac 3416d0
+			return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+                tmp1[fromlen - 1] = '.';
Adam Tkac 3416d0
+        }
Adam Tkac 3416d0
+        tmp1[fromlen] = '\0';
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	if (idna_to_unicode_lzlz (tmp1, &tmp2, 0) != IDNA_SUCCESS)
Adam Tkac 3416d0
+		return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+	(void) strcpy (tmp1, tmp2);
Adam Tkac 3416d0
+	free (tmp2);
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+        tolen = strlen(tmp1);
Adam Tkac 3416d0
+        if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
Adam Tkac 3416d0
+                tolen--;
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+        if (isc_buffer_length(buffer) < used_org + tolen)
Adam Tkac 3416d0
+                return (ISC_R_NOSPACE);
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+        isc_buffer_subtract(buffer, isc_buffer_usedlength(buffer) - used_org);
Adam Tkac 3416d0
+        memcpy(isc_buffer_used(buffer), tmp1, tolen);
Adam Tkac 3416d0
+        isc_buffer_add(buffer, tolen);
Adam Tkac 3416d0
+
Adam Tkac 3416d0
+        return (ISC_R_SUCCESS);
Adam Tkac 3416d0
+}
Adam Tkac 3416d0
+#endif /* WITH_LIBIDN*/
Adam Tkac 3416d0
 
Adam Tkac 3416d0
 #ifdef DIG_SIGCHASE
Adam Tkac 3416d0
 void