Adam Tkac 2fc1c6
diff -up bind-9.7.0b1/bin/dig/dighost.c.libidn bind-9.7.0b1/bin/dig/dighost.c
Adam Tkac 2fc1c6
--- bind-9.7.0b1/bin/dig/dighost.c.libidn	2009-09-16 01:48:09.000000000 +0200
Adam Tkac 2fc1c6
+++ bind-9.7.0b1/bin/dig/dighost.c	2009-10-20 10:49:26.719056220 +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 2fc1c6
@@ -153,6 +158,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 2fc1c6
@@ -1184,6 +1197,9 @@ setup_system(void) {
Adam Tkac 3416d0
 	dig_searchlist_t *domain = NULL;
Adam Tkac 3416d0
 	lwres_result_t lwresult;
Adam Tkac 353581
 	unsigned int lwresflags;
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 2fc1c6
@@ -1242,8 +1258,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 2fc1c6
@@ -1957,12 +1980,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 2fc1c6
@@ -1999,6 +2028,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 2fc1c6
@@ -2018,6 +2057,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 2fc1c6
@@ -2044,6 +2092,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 2fc1c6
@@ -2099,6 +2161,13 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac e99cc3
 			result = dns_name_fromtext(lookup->name, &b,
Adam Tkac e99cc3
 						   dns_rootname, 0,
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 e99cc3
+						   dns_rootname, 0,
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 2fc1c6
@@ -3617,7 +3686,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 2fc1c6
@@ -3656,6 +3725,10 @@ destroy_libs(void) {
Adam Tkac bbeea4
 	result = dns_name_settotextfilter(NULL);
Adam Tkac bbeea4
 	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 2fc1c6
@@ -3834,6 +3907,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
Adam Tkac 2fc1c6
diff -up bind-9.7.0b1/bin/dig/Makefile.in.libidn bind-9.7.0b1/bin/dig/Makefile.in
Adam Tkac 2fc1c6
--- bind-9.7.0b1/bin/dig/Makefile.in.libidn	2009-09-22 10:47:55.000000000 +0200
Adam Tkac 2fc1c6
+++ bind-9.7.0b1/bin/dig/Makefile.in	2009-10-20 10:50:06.201543709 +0200
Adam Tkac e99cc3
@@ -46,10 +46,10 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} 
Adam Tkac 353581
 		${LWRESDEPLIBS}
Adam Tkac 353581
 
Adam Tkac 2fc1c6
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
Adam Tkac 2fc1c6
-		${ISCLIBS} @IDNLIBS@ @LIBS@
Adam Tkac 2fc1c6
+		${ISCLIBS} @IDNLIBS@ @LIBS@ -lidn
Adam Tkac 353581
 
Adam Tkac 2fc1c6
 NOSYMLIBS =	${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
Adam Tkac 2fc1c6
-		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@
Adam Tkac 2fc1c6
+		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@ -lidn
Adam Tkac e99cc3
 
Adam Tkac 353581
 SUBDIRS =
Adam Tkac 353581
 
Adam Tkac e99cc3
@@ -67,6 +67,8 @@ HTMLPAGES =	dig.html host.html nslookup.
Adam Tkac 353581
 
Adam Tkac 353581
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
Adam Tkac 353581
 
Adam Tkac 353581
+EXT_CFLAGS = -DWITH_LIBIDN
Adam Tkac 353581
+
Adam Tkac 353581
 @BIND9_MAKE_RULES@
Adam Tkac 353581
 
Adam Tkac 353581
 dig@EXEEXT@: dig.@O@ dighost.@O@ ${UOBJS} ${DEPLIBS}